Forum Moderators: open

Message Too Old, No Replies

JavaScript Script File Switcher

dynamically changing the used value of a <script> tag's src attribute

         

createErrorMsg

9:44 pm on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm working on the interface of a CMS. I've added JS quicktags to it and would like to be able to toggle between two different versions of the quicktags file...one providing buttons for one set of elements, the other providing buttons for a different set of elements.

I thought a relatively simple way to do this would be to have two seperate JS files for the two quicktag sets and provide a link to toggle between the JS files. Unfortunately, it turns out it's not that easy.

I've tried adding an ID to the script tag, then changing the value of the src attribute...

function change(){
var script;
script = document.getElementById('script1');
script.src = "blah.js";
}
function check(){
var source;
var script;
script = document.getElementById('script1');
source = script.getAttribute('src');
alert(source);
}

In testing the resulting value of the SRC attribute (the check() function), the script IS changing the attribute. But, not surprisingly, it isn't changing the JS file being subsequently used by the page.

I suppose that makes sense, since the page isn't reloading, but unfortunately, this puts me at the limit of my js coding skills.

Any tips on a new approach to take to make a javascript SCRIPT file switcher?

Thanks in advance,

cEM

Rambo Tribble

10:35 pm on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using document.createElement(), setting the src and then employing appendChild you can cause a new script file to be loaded. If it contains functions with the same names as a previously loaded file, they should overwrite the previous.