Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

run JavaScript that is part of AJAX response

6 views
Skip to first unread message

peterm...@gmail.com

unread,
Apr 12, 2006, 1:23:02 AM4/12/06
to
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

Ian Collins

unread,
Apr 12, 2006, 1:50:24 AM4/12/06
to
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.

peterm...@gmail.com

unread,
Apr 12, 2006, 2:07:04 AM4/12/06
to

Ian Collins wrote:

> peterm...@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

Ian Collins

unread,
Apr 12, 2006, 2:35:38 AM4/12/06
to
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.

peterm...@gmail.com

unread,
Apr 12, 2006, 12:19:47 PM4/12/06
to

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.

peterm...@gmail.com

unread,
Apr 12, 2006, 12:29:17 PM4/12/06
to
peterm...@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>

Lasse Reichstein Nielsen

unread,
Apr 12, 2006, 1:17:13 PM4/12/06
to
peterm...@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 - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

peterm...@gmail.com

unread,
Apr 12, 2006, 1:46:59 PM4/12/06
to
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>

Tony

unread,
Apr 12, 2006, 1:49:13 PM4/12/06
to

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.

Lasse Reichstein Nielsen

unread,
Apr 12, 2006, 9:11:49 PM4/12/06
to
peterm...@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.

peterm...@gmail.com

unread,
Apr 13, 2006, 12:48:30 AM4/13/06
to

Lasse Reichstein Nielsen wrote:
> peterm...@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

Lasse Reichstein Nielsen

unread,
Apr 13, 2006, 7:04:53 AM4/13/06
to
peterm...@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.

cool2005

unread,
Apr 20, 2006, 5:11:08 AM4/20/06
to
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

0 new messages