Closed Thread Icon

Preserved Topic: Changing script source (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18320" title="Pages that link to Preserved Topic: Changing script source (Page 1 of 1)" rel="nofollow" >Preserved Topic: Changing script source <span class="small">(Page 1 of 1)</span>\

 
stinx
Bipolar (III) Inmate

From: London, UK
Insane since: Apr 2002

posted posted 07-06-2002 04:08

Ok, I haven't much experience in mozilla (or any browser apart from IE), so maybe someone could help me out here.

I want to be able to change the source of a linked javascript file. I can get this to work in IE quite easily with the following:

code:
<script id="varscript" language="JavaScript" src="script1.js"></script>
<script language="JavaScript">
function change_src()
{
varscript.src = "script2.js";
}
</script>
<button onClick="change_src();">Change Source</button>



change_src() is called as an event handler. script1.js and script2.js each contain different versions of another function, alert_src() as follows:

code:
// In script1.js
function alert_src()
{
alert("This is script 1");
}
alert_src();


code:
// In script2.js
function alert_src()
{
alert("This is script 2");
}
alert_src();



When the event handler is called, the function gets redefined according to the new script. Well, at least in IE it does. Also, the function should run where it is called just after the definition (this is a key part of what I am trying to do)

I've tried using

code:
document.getElementById("varscript").setAttribute("src", "script2.js");

amongst other attempts in mozilla, and while it appears that the src is changed, the function does not become redefined and the script doesn't run.

Anyone have any idea as to how to make this cross browser compatible?


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-06-2002 07:50

Ironicly I was just reading about this on DHTMLcentral He uses this technique for dynamically changing data with out using an iframe buffer.

Basically the diff between IE and Mozilla (and the issue you are having) is that in mozilla you can't just change the src tag.

You have to remove the current element using a refrence to the script within the head tag

grab the head info
head = document.getElementsByTagName('head').item(0);

remove the existing script
head.removeChild('varscript');

create a new one
script = document.createElement('script');

and set the src
script.src = "script2.js";






.:[ Never resist a perfect moment ]:.

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 07-06-2002 11:43

Or simply like this:

code:
<SCRIPT LANGUAGE="JavaScript">
//This script defines the file nav.js as the one to
//use if the browser version is 4 or higher,
//if the version is below 4 it uses nav2.js
//as the file to include as src.
var version=parseInt(navigator.appVersion);
if (version >= 4){
theScriptSrc = "nav.js";
}else{
theScriptSrc = "nav2.js";
}
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
//This part replaces the earlier SCRIPT SRC
//and should be placed where you want the script to be included (ON ONE LINE, PLEASE!).
document.write('<scr'+'ipt language="javascript" type="text/javascript" src="' + theScriptSrc + '"></SCR'+'IPT>');
</SCRIPT>



Not that dynamic, but that part should be quite easy to fix...
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

[This message has been edited by DmS (edited 07-06-2002).]

stinx
Bipolar (III) Inmate

From: London, UK
Insane since: Apr 2002

posted posted 07-06-2002 14:21

Thanks bitdamaged - you're a saint! This is exactly what I needed (and for the same reasons).

Now all I have to do is check it works in all versions of netscape...

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 07-06-2002 19:52

stinx: bitdamaged's code won't work in Netscape 4.x because it uses DOM funtions which NN 4 doesn't know.

But DmS' version should work in every browser (version 4 and higher).

kuckus (cell #282)

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 07-12-2002 22:24

I use that method aswell ( document.write(js source file) )with big scripts, what I do is I have function like moveXY() sizeXY() and other things. Each different depending if its IE or gecko (screw NS4, he he or at least with my webpage anyway, the document.write function works in ns, and nothing on my page wont work with NS except some opacity stuff, but Im just that extra bit lazy).

Then I have a second js file for stuff that will be the same in all browsers.
Why ?: The user downloads less, it gets rid of a load of if(IE bla bla) so thats even less processor work. Then you think "But then if you alter the script you have to change it two/three times", thats why I have a second script file for everything else that might change.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-12-2002 23:13

Woah, you can do that? Just create a script node/element, and the browser loads the script?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-12-2002 23:34

yeppers cool eh?

guy's the link I posted does something much different from what DMS' does. His is fine for loading different scripts when the page loads depending on certain variables. This script however allows you to actually go out and grab new script's dynamically after the page loads. Which means you could create a whole dynamically loaded site that never switches pages. It can go grab functionality and content as it goes.



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-12-2002 23:45

(Remembering, of course, that it would be completely innaccessible to anyone with javascript turned off.)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-13-2002 02:11

stupid neandrathals



.:[ Never resist a perfect moment ]:.

« BackwardsOnwards »

Show Forum Drop Down Menu