473,847 Members | 1,542 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 12478
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
15326
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
3231
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
4908
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
2458
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
4070
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
2114
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
9017
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
2572
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
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10996
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...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5730
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...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4541
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
2
4133
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3171
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.