run JavaScript that is part of AJAX response

P

petermichaux

Hi,

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?

Thanks,
Peter
 
I

Ian Collins

Hi,

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?
I think the only cross browser solution is to extract the script and
eval() it, so long as you are sure of the source of the script.
 
P

petermichaux

Ian said:
I think the only cross browser solution is to extract the script and
eval() it

Hi Ian,

I'm not opposed to eval() for this but thought there might be an
officially sanctioned solution that didn't use eval().

I tried to be sneaky and use innerHTML to insert the fragment. Then use
getElementsByTagName to extract the script elements. This didn't work
so well. I'm still trying to figure out what is going on.

Do you usually extract using some regular expression match on the
script tags in the raw response? Is there an especially easy way to get
the contents of each script tag pair into an array for eval() of each?

Thanks,
Peter
 
I

Ian Collins

Hi Ian,

I'm not opposed to eval() for this but thought there might be an
officially sanctioned solution that didn't use eval().

I tried to be sneaky and use innerHTML to insert the fragment. Then use
getElementsByTagName to extract the script elements. This didn't work
so well. I'm still trying to figure out what is going on.

Do you usually extract using some regular expression match on the
script tags in the raw response? Is there an especially easy way to get
the contents of each script tag pair into an array for eval() of each?
I prefer JSON for AJAX applications.

Have you tried sending back a file name, creating a script element,
setting the type (text/javascript) and src (file name) attributes?
 
P

petermichaux

Ian said:
I prefer JSON for AJAX applications.

I'm trying to use the Yahoo! UI connection library with a Rails server
application. It is very natural for a Rails app to return html string
so I want to try to get this combination working.
Have you tried sending back a file name, creating a script element,
setting the type (text/javascript) and src (file name) attributes?

I don't think that will work so well in this situation because the
Rails app is dynamically creating the response. I'd have to save that
to disk first. Besides, shouldn't the following work anyway?

I tried to find the script strings in the AJAX response text with the
following. "o" is the variable holding the ajax response object that
Yahoo connection lib supplies. o.responseText is the string
representing the response. This doesn't seem to find any scripts even
thought I know a script is in the string.

var spt = o.responseText.match(/<.*?script.*?>.*?<\/.*?script.*>/gm);

This regular expression works in a little test Ruby script I tried.

Any ideas?

Thanks,
Peter

The Yahoo lib also has o.responseXML which is a DOM fragment as far as
I know. Using this doesn't work for me either.
 
P

petermichaux

I tried to find the script strings in the AJAX response text

I should have posted this little JavaScript example that doesn't work.
I get a matches has no properties error.

<script language="JavaScript">
var str = "some html <script language=\"JavaScript\"> some \n code
<\/script> now some more html"
var matches = str.match(/<.*?script.*?>.*?<\/.*?script.*>/gm);
document.write(matches[0]);
</script>
 
L

Lasse Reichstein Nielsen

(e-mail address removed) writes:

[extract script contents from HTML source]
var spt = o.responseText.match(/<.*?script.*?>.*?<\/.*?script.*>/gm);

I would reduce the RegExp to:
var re = /<script\b.*?>(.*?)<\//ig; // assumes HTML well formed
and then loop through it as:
var match;
while (match = re.exec(htmlString)) {
eval(match[1]);
}

/L
 
P

petermichaux

Hi Lasse,

Thanks for the code snip. I haven't used ReExp.exec() before and it
seems to be exactly what I want.

I tried you suggestion in my example. It works fine if the script tags
do not contain a line break. If there is a new line break and I add the
multiline flag on the regexp I don't get any matches. Any ideas why?

Thanks again,
Peter


<script language="JavaScript">

var str = "some html <script language=\"JavaScript\"> some \n code
<\/script> now some more html"
var re = /<script.*?>(.*?)<\//igm;
var match;
while (match = re.exec(str)) {
document.write(match[1]);
}

document.write("goodbye");

</script>
 
L

Lasse Reichstein Nielsen

If there is a new line break and I add the
multiline flag on the regexp I don't get any matches. Any ideas why?

The multiline flag means that "^" and "$" matches start/end of lines
instead of start/end of entire string. Won't help us here.
Change "." to "[\s\S]" in the regexp to also match linebreaks.

/L
 
P

petermichaux

Lasse said:
If there is a new line break and I add the
multiline flag on the regexp I don't get any matches. Any ideas why?

The multiline flag means that "^" and "$" matches start/end of lines
instead of start/end of entire string. Won't help us here.
Change "." to "[\s\S]" in the regexp to also match linebreaks.

Does RegExp syntax change between languages? Seems like Ruby and
JavaScript are different in this case. In Ruby the mulitline flag makes
it so that '.' will match '\n'.

Thanks again for the info.

Peter
 
L

Lasse Reichstein Nielsen

Does RegExp syntax change between languages?

Absolutely, both the actual syntax and the flags that apply to it.
Javascript has only the following flags:
i : ignore case
g : global (allows the same regexp to be used to match a string more
than once, keeping track of the index it got to)
m : multiline (allows ^ and $ to match begin and end of lines too)
Seems like Ruby and JavaScript are different in this case. In Ruby
the mulitline flag makes it so that '.' will match '\n'.

The specfication of Javascript (the ECMAScript standard) specifies the
meaning. If multiline is false ("m" flag absent), then "^" matches
only at index 0. If true, it matches also after a line terminator.
Symmetrically for "$". The matching for "." makes no mention of the
multiline property.

/L
 
C

cool2005

I have been using hidden frames (from real frames to dynamic iframes)
and don't see any reason that
people introduced AJAX: anything can be don't be AJAX can be done by
hidden frames but not the other
way around. One of the major problems with AJAX is by passing the core
of browsers, the html rendering.
Hidden frames is the solution to your problme. See more details at

http://www.NewFramework.com

mark
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top