473,382 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

'executing code retreived by XMLHTTPRequest

I am having some difficulties getting some inline js code to run when
retrieved via AJAX by XMLHTTPRequest.

Basically I am running a php script every 15 secs or so that does a query of
a mysql database, checks to see if any new chat requests have been sent to
the user, and if so, it is simply returning them via a "echo" statement, and
the info is placed into a DIV in my sidebar.

Now when I add something in like:
<script type="text/javascript">
alert('you have a chat request');
</script>

it runs if i go ahed and run the script by itself, i get the chat requests
and the alert to popup. When the script is called via AJAX and loaded into
the div, I get the message from the mysql query about chat info, but no
javascript alert.

Am i just missing something stupid here? Like I said, it works when I run
the script by itself, just not when called via XMLHTTPRequest.

Thanks for any help.
Oct 27 '07 #1
4 1662
Dave said the following on 10/26/2007 9:38 PM:
I am having some difficulties getting some inline js code to run when
retrieved via AJAX by XMLHTTPRequest.

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/7120355459a853f1/994ddde766fc0caf?lnk=gst&q=loadhtmlfragment#994ddd e766fc0caf>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 1 '07 #2
On Oct 26, 6:38 pm, "Dave" <d...@nospam.ever.comwrote:
I am having some difficulties getting some inline js code to run when
retrieved via AJAX by XMLHTTPRequest.

Basically I am running a php script every 15 secs or so that does a query of
a mysql database, checks to see if any new chat requests have been sent to
the user, and if so, it is simply returning them via a "echo" statement, and
the info is placed into a DIV in my sidebar.

Now when I add something in like:
<script type="text/javascript">
alert('you have a chat request');
</script>
If the server response is just JavaScript like

alert('you have a chat request');

then you can do this

eval(xhr.responseText);

This opens a huge can of worms about the scope in which the evaluated
JavaScript executes. The tread Rand Webb has linked may discuss that.
If not search his name and "script insertion" in the archives for a
very different approach he uses. The XHR technique like you are trying
is more popular however. Which is better has been debated here before.

Peter

Nov 1 '07 #3
Thanks for the help guys!

I ended up dropping in Randy's code and it worked like a charm.
Nov 3 '07 #4
Dave said the following on 11/3/2007 9:56 AM:
Thanks for the help guys!

I ended up dropping in Randy's code and it worked like a charm.
The code I wrote, as it is now, still has a few flaws in it that need to
be modified. Add an onload event handler to window or body and retest
it. That is but one flaw I need to address.

What I really need to do is sit and write a page about it and post a URL
to it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 3 '07 #5

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

Similar topics

4
by: seans | last post by:
Hi, Is there some way from preventing the JavaScript code in a document loaded into an iframe from executing? I don't have access to the pages being loaded into the i-frame so I can't modify...
5
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
2
by: Jon Slaughter | last post by:
When a user clicks a link I have it open up a file in a div using ajax. my code is <a href="#Find" onclick="javascript:jah('Find.html','content');">Find</a><br /> Where Find.html is an html...
3
by: Aussie Rules | last post by:
Hi. I am using Live Earth SDK, and have very little Javascript knowledge, but I need to insert values into a Javascript function, where the vales are in the ASP.net VB code. The javascript...
15
by: rage3324 | last post by:
I am posting html onto my main page between div tags using xmlhttprequest and innerhtml. The html I am posting has javascript inside which I am executing using the eval() function. However, the...
3
by: Kirk | last post by:
Let me start by saying that I am a complete idiot when it comes to JS. However, I need help with something that apparently can only be done this way. I am using an ASP.NET AJAX control...
7
by: Brent | last post by:
I'm trying to figure out how to iterate over an array that would send a series of XMLHttp requests. I'd like to have each request finish before the next one begins, but I believe that this is not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.