473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 22312
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sgmaat | last post by:
I have create a AJAX webpage where if you press a link content is loaded into a div. NOw when this content is loaded no javascript is processed (i.e <Script>alert(..);</script>), but if the...
4
by: Barry | last post by:
Hi i have the following class Public Class ShipmentClass Public RatedShipment As RatedShipmentClass() Public Class RatedShipmentClass Public GuaranteedDaysToDelivery As String Public...
2
by: Yorian | last post by:
Hi, Another problem another question (as usual). I want to retrieve data through ajax, within the data there is some javascript which needs to be used (I figured that one out using eval ())....
10
by: Tom Cole | last post by:
While I've done javascript work for as long as I can remember (since Netscape first released it), I've only ever used it for trivial things, change a color here, validate a text element there, blah...
6
by: lucyh3h | last post by:
Hi, In one of my pages, I use javascript (AJAX) to populate one mulitple select field based on user's click event on another control. I noticed that when I navigate back to this page by clicking...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
4
by: jake | last post by:
Can someone please give me an example on how to call a wcf method that resides on a different domain from JavaScript (not from C#/asp.net)? All the examples that I have seen show how to call from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.