Computing.Net > Forums > Web Development > Loading external JS...

Loading external JS...

Reply to Message Icon

Original Message
Name: smumdax
Date: July 30, 2005 at 21:28:45 Pacific
Subject: Loading external JS...
OS: XP
CPU/Ram: whatever
Comment:

We load external Javascript files using
<script src="somefile.js"></script>

... but is there a way to load a JS file only when needed?
Simple situation is this:
I have 10 js files.
I want to load the #1. Use it. Unload.
Load the #2. Use it. Unload.
etc, etc.



Report Offensive Message For Removal

Response Number 1
Name: Michael J (by mjdamato)
Date: July 31, 2005 at 12:09:55 Pacific
Subject: Loading external JS...
Reply: (edit)

Not sure if this is what you need, but here is a technique I used to dynaimcally change the referenced external javascript file. Not sure how cross-platform capable it is, but works great in IE6.


First, give the javascript refernce an id:

<script src="somefile.js" id="extJS"></script>

Then create a function that you will call, when needed, to change the referenced js file. You can use whatever logic you need to determine the file needed. This example passes the js file name to the function.

function changejs(filename) {
document.getElementById('extJS').src = filename + '.js';
}

This was usefull for loading different data files based upon the criteria. You can also use it to have different functions with the same name - you determine which functions to use by the js file that you load.


Report Offensive Follow Up For Removal

Response Number 2
Name: smumdax
Date: August 1, 2005 at 08:27:28 Pacific
Subject: Loading external JS...
Reply: (edit)

..That seem to be quite what I need. Actually found something similar.

Thanks!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Loading external JS...

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software