469,926 Members | 1,812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

'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 1556
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jon Slaughter | last post: by
3 posts views Thread by Aussie Rules | last post: by
3 posts views Thread by Kirk | last post: by
7 posts views Thread by Brent | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.