I would like to know how to load .js files conditionally?
Suppose I have a Drop down box that is populated dynamically with A B C.
When I select Element A I want it to load a .js file, B a different .js file and C a different .js. The js. files are not simply A.js, they are a reference to an URL address where they are stored.
Expand|Select|Wrap|Line Numbers
- <select size='1'
- onchange="var si=this.selectedIndex; if(si){ addScript( this.options[si].value)}">
- <option>Select an Option...</option>
- <option value="temp.js">A</option>
- <option value="whatever.js">B</option>
- <option value="whatever2.js">C</option>
- </select>
- <script>
- function addScript(turl) {
- var xJs = document.createElement("script");
- xJs.type = "text/javascript";
- var h = document.getElementsByTagName("head");
- if (h && h[0]) {
- h[0].appendChild(xJs);
- }
- xJs.src = turl;
- return xJs;
- }
- </script>