469,366 Members | 2,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

run JavaScript that is part of AJAX response

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

Apr 12 '06 #1
12 22150
pe**********@gmail.com wrote:
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.

--
Ian Collins.
Apr 12 '06 #2

Ian Collins wrote:
pe**********@gmail.com wrote:
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


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

Apr 12 '06 #3
pe**********@gmail.com wrote:
Ian Collins wrote:
pe**********@gmail.com wrote:

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

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?

--
Ian Collins.
Apr 12 '06 #4

Ian Collins wrote:
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.

Apr 12 '06 #5
pe**********@gmail.com wrote:
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>

Apr 12 '06 #6
pe**********@gmail.com 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 12 '06 #7
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>

Apr 12 '06 #8
pe**********@gmail.com wrote:
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?


One solution was suggested - to extract the script and then use eval().

Another solution I found awhile back, when trying to do the same thing,
is to add the script to the <head>. Unfortunately, I can't recall how to
do this offhand. You will still need to extract the script, then you
will create a new script tag in the <head> and place the javascript you
want to execute in that.

....

Did a quick search on "javascript include" and came up with this:
http://www.phpied.com/javascript-include/
Scroll down a bit to "The DOM way" - that's what I did, and it worked
nicely. There are a few more options after that one that may help, too.
Apr 12 '06 #9
pe**********@gmail.com writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 13 '06 #10

Lasse Reichstein Nielsen wrote:
pe**********@gmail.com writes:
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

Apr 13 '06 #11
pe**********@gmail.com writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 13 '06 #12
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

Apr 20 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by sgmaat | last post: by
4 posts views Thread by Barry | last post: by
2 posts views Thread by Yorian | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.