'executing code retreived by XMLHTTPRequest

D

Dave

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.
 
P

Peter Michaux

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
 
B

Bart Van der Donck

Dave said:
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.

Since you are working on a chat application, I would say there is
probably not that much to execute. One strategy might consist of
inventing a data consensus for server and client. It might be a good
choice to use XML in this case (which was actually the original goal
of XMLHttpRequest), e.g.

<transfer>
<data action="display_on_chat">Hello</data>
</transfer>

<transfer>
<data action="do_alert">Important!</data>
</transfer>

Then load the response into the XML parser, and read out how the data
should be displayed with a few if-else constructions, something like:

if (action == 'do_alert') alert(string);
if (action == 'display_on_chat') document.write(string);

Hope this helps,
 
D

Dave

Thanks for the help guys!

I ended up dropping in Randy's code and it worked like a charm.
 
S

Stevo

Bart said:
if (action == 'display_on_chat') document.write(string);
Bart

I haven't followed this thread, but that line of code above can't be a
good idea on a closed page.
 
B

Bart Van der Donck

Stevo said:
I haven't followed this thread, but that line of code above can't be a
good idea on a closed page.

Correct - I just wanted to demonstrate the rough principle. For the
chat application, perhaps the original poster would want to update the
contents of a textarea, div, iframe,... stuff like that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top