473,412 Members | 4,957 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,412 software developers and data experts.

Dynamically switch Javascript

Hi,

I have an interesting problem that I am stuck on and was wondering if there
might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will be
well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may be a
string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.

I have tried using "eval()" and also constructing a script node dynamically
and both have not worked for me.

Thanks in advance for your help!

-- wp
Sep 25 '06 #1
6 3453
William Pierce wrote:
Hi,

I have an interesting problem that I am stuck on and was wondering if there
might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will be
well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may be a
string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.

I have tried using "eval()" and also constructing a script node dynamically
and both have not worked for me.
You should add a dynamic script tag to the document head, rather than
within the body. I don't know whether this will work using innerHTML,
I've only used DOM calls to add scripts.

--
Ian Collins.
Sep 25 '06 #2
Ian Collins said the following on 9/25/2006 6:24 PM:
William Pierce wrote:
>Hi,

I have an interesting problem that I am stuck on and was wondering if there
might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will be
well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may be a
string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.

I have tried using "eval()" and also constructing a script node dynamically
and both have not worked for me.
You should add a dynamic script tag to the document head, rather than
within the body. I don't know whether this will work using innerHTML,
I've only used DOM calls to add scripts.
The only browser that executes script blocks that are inserted via
innerHTML is NS6 and not all releases of NS6 execute them (only the
earliest releases did). You are right about the createElement approach
as being the best solution.

--
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 25 '06 #3
William Pierce said the following on 9/25/2006 4:46 PM:
Hi,

I have an interesting problem that I am stuck on and was wondering if there
might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will be
well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may be a
string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.
Insert your text string via innerHTML and then strip out the script
elements, create new elements, appendChild them and get them executed.

var d =
document.getElementById('divContents').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChild (newScript);
}

With this HTML:

<div id="divContents"></div>

--
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 25 '06 #4
Ian,

Thanks for your reply. Let me tell you what I did:

a) My Javascript file "test.js" contains the following code:
function appendChild(node, text) {
if (null == node.canHaveChildren || node.canHaveChildren) {
node.appendChild(document.createTextNode(text));
} else {
node.text = text;
}
}
function Doit()
{
var scr1="<script language=\"JavaScript\"
src=\"http://path/to/url1\"></script>";
var scr2="<script language=\"JavaScript\"
src=\"http://path/to/url2\"></script>";
var x = 1;
var divstring = "";
//dummy just to illustrate dynamic selection of one or the other script
tag...
if ( x == 1 ) {
divstring = scr1;
}
else {
divstring = scr2;
}
var script = document.createElement("script");
script.setAttribute("type","text/javascript");
appendChild(script, document.createTextNode(divstring));
document.getElementsByTagName("head")[0].appendChild(script);
}

b) My HTML page that calls the Javascript code does the following:

It simply calls the function "Doit()" from within the page.

I have two issues with this:

a) First, I get an error from IE complaining about an undefined object when
trying to load the HTML page.

b) How do I assign the content produced by executing the chosen Javascript
to the innerHTML of the "div_2" tag?

Any help you can provide would be great!
wp

"Ian Collins" <ia******@hotmail.comwrote in message
news:4n************@individual.net...
William Pierce wrote:
>Hi,

I have an interesting problem that I am stuck on and was wondering if
there
might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will
be
well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may
be a
string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.

I have tried using "eval()" and also constructing a script node
dynamically
and both have not worked for me.
You should add a dynamic script tag to the document head, rather than
within the body. I don't know whether this will work using innerHTML,
I've only used DOM calls to add scripts.

--
Ian Collins.

Sep 26 '06 #5
Hi, Randy:

Thanks for your response. I followed your suggestions.

Just to refresh, I have two Javascript strings:

var scr1='<script language="JavaScript" src=http://url1></script>';
var scr2='<script language="JavaScript" src=http://url2></script>';

Now I want to decide at page load time which one of these to process, and
to load the contents of the executed Javascript into my div tag.

So I followed your suggestion:

document.getElementById('div_2').innerHTML = divstring; //divstring is set
to either scr1 or scr2

var d =
document.getElementById('divContents').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
newScript.src = d[x].src; // I added this because my
Javascript does not have any text, just a src attribute...
document.getElementById('divContents').appendChild (newScript);
}

The HTML page simply has <div id='div_2'></div>

I think this was a faithful copy of what you suggested. Let me know if I
made a mistake.

The problem is this: The d.length is 0. So the loop does not even execute.

Any ideas?

One other issue....In my case, the Javascript code does not have any text in
it...just a reference to a src attribute. So I set that

"Randy Webb" <Hi************@aol.comwrote in message
news:Ut******************************@comcast.com. ..
William Pierce said the following on 9/25/2006 4:46 PM:
>Hi,

I have an interesting problem that I am stuck on and was wondering if
there might be others who can shed some light....

Basically, within an HTML page, I have a tag that looks as follows:
<div id="xxx">

</div>

A call gets made to a server, and it returns a string. The string will
be well-formed HTML. In most cases, all I do is

document.getElementById("xxx").innerHTML = <string>

However, sometimes, the string that may be returned by the server may
be a string that looks as follows:
<script language="JavaScript" src="path/to/js"></script>

In this case, I need to be able to execute this Javascript and place its
contents inside the "div" tag.

Insert your text string via innerHTML and then strip out the script
elements, create new elements, appendChild them and get them executed.

var d =
document.getElementById('divContents').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChild (newScript);
}

With this HTML:

<div id="divContents"></div>

--
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 26 '06 #6
William Pierce said the following on 9/25/2006 9:15 PM:
Hi, Randy:

Thanks for your response. I followed your suggestions.
Then please don't top-post here.
Just to refresh, I have two Javascript strings:

var scr1='<script language="JavaScript" src=http://url1></script>';
var scr2='<script language="JavaScript" src=http://url2></script>';

Now I want to decide at page load time which one of these to process, and
to load the contents of the executed Javascript into my div tag.
Why not just document.write the string you want?

<script type="text/javascript">
var scriptToUse = scr1;
if (someConditionToUseScript2IsTrue){
scriptToUse = scr2;
}
document.write(scriptToUse);
</script>

Or, is there more you aren't explaining?

P.S. Drop the language attribute and use the type attribute.

--
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 26 '06 #7

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

Similar topics

4
by: Red | last post by:
I have an array which is dynamically generated by parsing a web page: titles Array ( => bookmarks => New Folder => New Folder2 ) and I want to insert html links into another,...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: Henry | last post by:
I want my VB.Net windows application to be able to dynamically switch to either a Production environment or a test environment. Currently my SQL Connection string is hard coded in the data adapter...
1
by: Beegee | last post by:
Hi Happy 2006 I want to load the user control dynamically. I want to change to different usercontrol page to the same place holder accordingly. If there is no way to do so anyone please...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
5
by: BeanWorks | last post by:
Hi, I've been looking through books and posts, but so far I can't find anything that really addresses this problem. I have a frameset page with three frames, one on top with static content, and...
5
by: maya | last post by:
hi, I'm using this script to switch stylesheets dynamically (in response to user input..) http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm (sorry can't show url, am in...
4
daJunkCollector
by: daJunkCollector | last post by:
I am using AJAX to dynamically load content into a <div> layer. As it stands, I am loading .html's into the <div> layer and it works awesome. The problem is that when I try to replace the .html...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
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
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...
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,...
0
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...

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.