[Javascript] reassign the script src attribute

Mike Dougherty mdougherty at pbp.com
Wed Aug 17 16:33:12 CDT 2005


That sounds very similar to my test :)

I was able to have this work: (test() is an alert function in f1.js and f2.js files)

<script type='text/javascript'>
	var s = document.createElement('script')
	s.type = 'text/javascript' ;
	if ( confirm("1=ok, 2=cancel:") ) {
		s.src = '/test/f1.js';
		}
	else {
		s.src = '/test/f2.js' ;
		}
	document.getElementsByTagName('head')[0].appendChild(s);
</script>

<script type='text/javascript'>
   test();
</script>

On Wed, 17 Aug 2005 15:44:02 -0500
  Triche Osborne <wdlists at triche-osborne.com> wrote:
> 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
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 
> 
> __________________________________________________________
> This message was scanned by ATX
> 4:25:49 PM ET - 8/17/2005




More information about the Javascript mailing list