473,396 Members | 2,109 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,396 software developers and data experts.

inserting scripts into the DOM

Hi,

My AJAX response is a bit of HTML that I insert into the DOM. I extract
the contents of the script elements in the response into an array
called aScripts. I then want to append these to an element so that the
scripts will actually execute. I am trying a variation on what Randy
Webb posted a few months ago.

function insertScripts(element, aScripts) {
if(typeof element == "string") {element =
document.getElementById(element);}
for (var i=0, t=aScripts.length; i<t; i++) {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = aScripts[i];
element.appendChild(newScript);
//newScript.appendChild(document.createTextNode(aScr ipts[i]));
//eval(aScripts[i]);
}
};

If I uncomment the eval() line then the scripts actually do execute but
using eval() defeats the purpose of Randy's suggested solution. It
seems like the "text" property is non-standard.

Any way to rescue this bit of code?

Thank you,
Peter

Sep 6 '06 #1
2 1579
pe**********@gmail.com said the following on 9/6/2006 4:41 AM:
Hi,

My AJAX response is a bit of HTML that I insert into the DOM. I extract
the contents of the script elements in the response into an array
called aScripts. I then want to append these to an element so that the
scripts will actually execute. I am trying a variation on what Randy
Webb posted a few months ago.

function insertScripts(element, aScripts) {
if(typeof element == "string") {element =
document.getElementById(element);}
for (var i=0, t=aScripts.length; i<t; i++) {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = aScripts[i];
element.appendChild(newScript);
//newScript.appendChild(document.createTextNode(aScr ipts[i]));
//eval(aScripts[i]);
}
};

If I uncomment the eval() line then the scripts actually do execute but
using eval() defeats the purpose of Randy's suggested solution. It
seems like the "text" property is non-standard.
The problem isn't the "text" property. It is directly related to your
parameter name of "aScripts"
Any way to rescue this bit of code?
aScripts = new Array()
aScripts[0] = "alert('hi with tags')"
aScripts[1] = '<script type="text/javascript">alert(\'hi with
tags\')<\/script>'

function insertScripts(element) {
if(typeof element == "string")
{element =document.getElementById(element);}
for (var i=0,t=aScripts.length; i<t; i++) {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = aScripts[i];
element.appendChild(newScript);
}
};

Calling that code as such:

insertScripts('scriptsDiv')

Does the same thing in IE7, Mozilla and Opera 9. I get the first alert,
I don't get the second one. IE throws a Syntax Error, Opera 9 Javascript
Console shows an error and points at the <script tag in the entry. So,
if your script snippet has a script tag still in it, it won't execute.

Mozilla totally dropped the ball on this one. It doesn't throw an error
(no messages in the console), it simply doesn't do *anything* after the
first alert.

Adding the eval call back in didn't change any of the behavior and that
leads to the passing of the array name. alert(t) and you will find that
it alerts 8 which is the length of the string "aScripts" and that is the
source of your problems. If you stop passing it as a parameter, it
remains the name of an Array. If you pass it as a parameter, then it's
not an Array unless you pass a reference to an Array but you said that
aScripts was the name of your Array.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 6 '06 #2

Randy Webb wrote:
pe**********@gmail.com said the following on 9/6/2006 4:41 AM:
Hi,

My AJAX response is a bit of HTML that I insert into the DOM. I extract
the contents of the script elements in the response into an array
called aScripts. I then want to append these to an element so that the
scripts will actually execute. I am trying a variation on what Randy
Webb posted a few months ago.

function insertScripts(element, aScripts) {
if(typeof element == "string") {element =
document.getElementById(element);}
for (var i=0, t=aScripts.length; i<t; i++) {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = aScripts[i];
element.appendChild(newScript);
//newScript.appendChild(document.createTextNode(aScr ipts[i]));
//eval(aScripts[i]);
}
};

If I uncomment the eval() line then the scripts actually do execute but
using eval() defeats the purpose of Randy's suggested solution. It
seems like the "text" property is non-standard.

The problem isn't the "text" property. It is directly related to your
parameter name of "aScripts"
Any way to rescue this bit of code?

aScripts = new Array()
aScripts[0] = "alert('hi with tags')"
aScripts[1] = '<script type="text/javascript">alert(\'hi with
tags\')<\/script>'

function insertScripts(element) {
if(typeof element == "string")
{element =document.getElementById(element);}
for (var i=0,t=aScripts.length; i<t; i++) {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = aScripts[i];
element.appendChild(newScript);
}
};

Calling that code as such:

insertScripts('scriptsDiv')

Does the same thing in IE7, Mozilla and Opera 9. I get the first alert,
I don't get the second one. IE throws a Syntax Error, Opera 9 Javascript
Console shows an error and points at the <script tag in the entry. So,
if your script snippet has a script tag still in it, it won't execute.

Mozilla totally dropped the ball on this one. It doesn't throw an error
(no messages in the console), it simply doesn't do *anything* after the
first alert.

Adding the eval call back in didn't change any of the behavior and that
leads to the passing of the array name. alert(t) and you will find that
it alerts 8 which is the length of the string "aScripts" and that is the
source of your problems. If you stop passing it as a parameter, it
remains the name of an Array. If you pass it as a parameter, then it's
not an Array unless you pass a reference to an Array but you said that
aScripts was the name of your Array.
Hi Randy,

Thank you. Your script works as advertised which I imagined was the
case. I was making a very foolish mistake. I was passing an element
(not a string) to insertScripts that I had just replaced. So element
was no long in the document and when I appeneded to it I was not
appending to the document.

Peter

Sep 6 '06 #3

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

Similar topics

4
by: Raj Kotaru | last post by:
Hi, In sqlplus, I can insert a single row using: insert into employee (name, salary, hiredate) values ('xyz', '86378', sysdate); Is there a modification of the insert command that will...
0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
8
by: Stephen Poley | last post by:
One disadvantage of using a simple text editor to produce HTML is that it is relatively time-consuming to put in the proper typographical quotation marks and dashes. A round tuit having arrived...
3
by: Patrick Olurotimi Ige | last post by:
I have a webform with DropDownLists and and i would like to have a link that would open up a window popup for the USER to insert some data into the Database and then on closing the popub window...
3
by: mjteigen | last post by:
I'm very new at Python, but have been trying it in conjunction with CGI. I've encountered a problem that I'm pretty sure is a trivial one, but I don't know enough Python to work it out. Here's an...
1
Ajm113
by: Ajm113 | last post by:
Ok, when I was new to this I had this problem and I bet a lot of other people did when they where new to PHP and Mysql. So this mite be your question; "Ok, no errors or warnings in mysql and php so...
2
by: SMH | last post by:
I am in the process of converting all HTML documents, including many dynamic/interactive documents, to XHTML documents (because I want to incorporate SVG and MathML, among other things). I am...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.