View Full Version : Import javascript in javascript
XandroZ
Apr 17th 2007, 9:36 am
I know that I can import an css file in other css file with @import
Ex: @import "style.css";
Can I do something like this in javascript, import a javascript in other javascript ;
Aragorn
Apr 18th 2007, 2:09 am
You can use
document.write("<script type='text/javascript' src='test.js'></scr"+"ipt>");
Note that you should split the closing script tag into two parts.
Or else you can use the DOM model to acheive the same
// Create the Script Object
var script = document.createElement('script');
script.src = 'server.js';
script.type = 'text/javascript';
script.defer = true;
script.id = 'scriptID'; // This will help us in referencing the object later for removal
// Insert the created object to the html head element
var head = document.getElementsByTagName('head').item(0);
head.appendChild(script);
Similary you can also remove a script object
var head = document.getElementsByTagName('head').item(0);
var old = document.getElementById('scriptID');
if (old) head.removeChild(old);
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.