473,796 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pass javascript in xmlHttp.respons eText

Is it possible to execute javascript as passed in xmlHttp.respons eText

Here is what I am doing:

search.js

var xmlHttp
xmlHttp=GetXmlH ttpObject()
var url="search.php "
xmlHttp.onready statechange=sta teChanged
xmlHttp.open("G ET",url,true)
xmlHttp.send(nu ll)

function stateChanged()
{

if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" )
{
document.getEle mentById("searc h").innerHTML=x mlHttp.response Text;
}

}

function GetXmlHttpObjec t()
{

var xmlHttp=null;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest( );
}

catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
}
catch (e)
{
xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
}
return xmlHttp;

search.php
<script type="text/javascript">
alert("hi");
</script>
<?php echo "hi"; ?>

test.php
<script src="search.js" ></script>
<div id=livesearch></div>

So what I see when I browse to test.php is 'hi' printed to the
browser, but I would also expect to have the javascript alert executed

If I visit search.php then I get 'hi' printed to the browser and the
alert message

So it seems that when I pass the response to the span the javascript
alert is ignored
(document.getEl ementById("sear ch").innerHTML= xmlHttp.respons eText;)

Sp my question is how can I have javascript be passed and executed?

Thanks!
Nov 4 '08 #1
2 12472
On Nov 5, 3:50 am, trp...@gmail.co m wrote:
Is it possible to execute javascript as passed in xmlHttp.respons eText
<snip>
Sp my question is how can I have javascript be passed and executed?
// One way is to manually find all new script tags and eval them:

var searchElement = document.getEle mentById("searc h");
searchElement.i nnerHTML=xmlHtt p.responseText;
var scripts = searchElement.g etElementsByTag Name('script');
for (var i=0;i<scripts.l ength;i++) {
eval(scripts[i].innerHTML);
}

Nov 5 '08 #2
On Nov 4, 2:50*pm, trp...@gmail.co m wrote:
Is it possible to execute javascript as passed in xmlHttp.respons eText
Yes. Even easier with responseXML.
>
Here is what I am doing:

search.js

var xmlHttp
xmlHttp=GetXmlH ttpObject()
Is that a constructor?
var url="search.php "
xmlHttp.onready statechange=sta teChanged
xmlHttp.open("G ET",url,true)
xmlHttp.send(nu ll)
Is your semicolon key broken?
>
function stateChanged()
{

if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" )
Lose that second test.
{
* * * * document.getEle mentById("searc h").innerHTML=x mlHttp.response Text;
Here is the problem. You could feature test the innerHTML property to
see if it will evaluate inline scripts, but IIRC, using standard DOM
methods (e.g. appendChild) avoids this problem altogether.

Alternatively, when you send your Ajax request, include a header to
indicate to the test.php script that it should send the document as
XHTML (with text/xml as the MIME type, IIRC.) Then you can import the
nodes as needed (even in IE.)
>
}
}

function GetXmlHttpObjec t()
Not a constructor.
{

var xmlHttp=null;

try
{
* * * * // Firefox, Opera 8.0+, Safari
And IE7, but slightly crippled.
* * * * xmlHttp=new XMLHttpRequest( );

}

catch (e)
Using try-catch in lieu of feature detection is not a good strategy.
{
* * * * // Internet Explorer
And anything else that failed above.
* * * * try
* * * * {
* * * * * * * * xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
* * * * }
* * * * catch (e)
* * * * {
* * * * * * * * xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
* * * * }}

return xmlHttp;

search.php
<script type="text/javascript">
* * *alert("hi");
</script>
<?php echo "hi"; ?>

test.php
<script src="search.js" ></script>
<div id=livesearch></div>

So what I see when I browse to test.php is 'hi' printed to the
browser, but I would also expect to have the javascript alert executed
Setting the innerHTML property does not execute the inline scripts in
IE (as well as other agents, IIRC.)
>
If I visit search.php then I get 'hi' printed to the browser and the
alert message
As expected.
>
So it seems that when I pass the response to the span the javascript
When you set the innerHTML property of the div.
alert is ignored
Right.

[snip]
Nov 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
15312
by: Don | last post by:
I'm writing an html page with some JavaScript that reads a web page into a client-side temp file, then reformats it, then submits that same file as a URL to the browser for display, via "locate.replace". I can do all this if the html page containing the script originates on the client machine, but don't know how to get access to a client-side temp file when the same html page containing the script is on a web host. How do I get access...
7
3228
by: julian.tklim | last post by:
Hi, I need to build an editable Datagrid with add & delete buttons on each row using javascript. DataGrid need not be pre-populated with values. To make the thing complicated, one of the column need to be a date picker field. I know things will be easier with ASPX datagrid.
2
4900
by: eros | last post by:
var xmlHttp; var m_placeholder; function executeProcess(serverscriptfile, placeholder, posts) { alert(posts); xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return;
6
2456
by: peace2007 | last post by:
Hi, I need to evaluate the return result of a function from Server.aspx as following. But it doesn't work as I need function getAjax() { var XmlHttp; //Creating object of XMLHTTP in IE try {
20
4048
RMWChaos
by: RMWChaos | last post by:
Currently testing in: WinVista / IE7 I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great and populates into the webpage; however, executing javascript has a few problems: 1. Opens on a blank page, but not a new page. It appears to compeltely overwrite the original page. I want it to update the existing page by loading within a <div>...
5
14013
by: JohnDriver | last post by:
Hi, I am having a form which has a text box and 3 radio buttons. I am using GET method in Ajax to pass the value. I can pass the value of the textbox fine but how to pass the value of radio button? I searched a few things on Google but I find the javascript below most useful in my case but there is some problem that my code is not running: var test = document.getElementsByName("meal"); for (i = 0; i < test.length; i++) {
7
2112
by: renphi | last post by:
I'm attempting display a DIV when clicking on a list item. This works, but only for the first list item. I realized that this was due to the ID not being unique so I made the IDs unique. My question is how do I pass this unique ID from each list item to the relevant javascript function so that it displays the DIV when clicked? Here's the javascript: // Create XmlHttp object. function createXHR() { try { return new XMLHttpRequest();...
12
9012
by: sarega | last post by:
Hi, <? #cd.php echo "<input type='text' name='text1' id='text1' onchange=\"return_value()"\ /><br></br>"; ?> Javascript file var xmlHttp; function return_value() { var x=document.getElementById('text1').value;
1
2569
by: kkshansid | last post by:
i want to pass both variables($q1 and value of select) from this php page to java script so that i can get both variables in second php file srt.php <script type="text/javascript" src="st.js"></script> <?php $q1=$_GET; require("conn.php"); $sql="SELECT distinct(".$q1.")FROM inst";
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.