473,573 Members | 3,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22330
pe**********@gm ail.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**********@gm ail.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
getElementsByTa gName 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**********@gm ail.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
getElementsByTa gName 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**********@gm ail.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="JavaS cript">
var str = "some html <script language=\"Java Script\"> 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**********@gm ail.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(htmlStr ing)) {
eval(match[1]);
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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="JavaS cript">

var str = "some html <script language=\"Java Script\"> 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**********@gm ail.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**********@gm ail.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2141
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 javascript is in <a onclick="..."> it will work. What i would like to know is: - Is this normal - What do i need to change so that javascript does get...
4
1381
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 ScheduledDeliveryTime As String Public Code As String
2
2195
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 ()). The problem is that the script I run (and get from the response) needs to use the response itself.
10
2658
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 blah blah. With Ajax (actually, the uncovering of the XmlHTTPRequest object) I absolutely see the benefit of moving more of the UI work to the...
6
4311
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 browser's "Back" button, the changes made to the multiple select is not preserved. (It shows the initial value). While all the other user...
3
3452
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 want to extract all the tracks from a specific CD. Right now, im using an array to stock all the data but its just a question of time before...
10
3447
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 ------------- I have perl script which simply runs a ps on a Solaris server and generates a static html page with all of the code perfectly and this html page...
4
5926
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 the same domain. I don't care what the binding is. I'll use what works. I need concrete examples or links to concrete example please. Not just...
0
7707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8086
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6432
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3737
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.