[Javascript] reassign the script src attribute

Triche Osborne wdlists at triche-osborne.com
Wed Aug 17 15:44:02 CDT 2005


Mike Dougherty wrote:
> Are there any caveats to reassigning a script tag's source attribute?
> 
> Is this possible:  (notice the SKU() function is different in each 
> source file)
> 
> <script id='productscript' src='productA.js' type='text/javascipt' 
>  ></script>
> <script type='text/javascript'>
>   if(productA){ var myobj = new SKU(); }
>   else {
>     document.getElementById('productscript').src = 'productB.js' ;
>     var myobj = new SKU();
>     }
> </script>

Hi Mike,
	I did some testing, but the problem seems to be that while the source 
attribute does change, the file itself doesn't attach. (I put an alert 
statement in each of the two script files, but outside of their 
functions, so I would know when/if each file loaded.)
	I thought of trying the technique used for preloading images, but when 
I tried to create a new HTMLScriptElement, the JS console throws an 
error that says "object cannot be created in this context." It makes 
sense that it would object to trying to create a script from within 
another, but I was curious.

Triche




More information about the Javascript mailing list