Javascript include
Moderator: General Moderators
Javascript include
Didn't somebody come up with something similar to include() but for javascript... I thought it was d11wqt, but I could be wrong... anybody know what I'm talking about? I searched the forums but came up empty...
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
you can create a DOM node for SCRIPT and set its src attribute for a similar functionality. I use something like following for cross-browser AJAX behavior
Code: Select all
// Get Base URL
URL = document.location.href;
xEnd = URL.lastIndexOf("/") + 1;
var Base_URL = URL.substring(0, xEnd);
scrObj = document.createElement('SCRIPT');
scrObj.type = 'text/javascript';
scrObj.src = Base_URL+'getContents.php?type=Gallery&cat='+Cat+'&subCat='+SubCat; //get Contents dynamically...
document.body.appendChild(scrObj);
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Javascript include
It was me, and it's basically n00b's method but beware that because the DOM won't be ready in Operra if the body isn't loaded you need to run it onload. My solution was this:The Ninja Space Goat wrote:Didn't somebody come up with something similar to include() but for javascript... I thought it was d11wqt, but I could be wrong... anybody know what I'm talking about? I searched the forums but came up empty...
Code: Select all
/**
* A JS file loader for SHANE
* @author Chris Corbyn
* @class Loader
* @constructor
*/
function Loader()
{
/**
* The paths to the files we want to load
* @type Array
*/
var files = new Array();
/**
* A list of files already loaded
* @type Array
*/
var active = new Array();
/**
* Becomes TRUE once window.onload has run
* @type Boolean
*/
var hasLoaded = false;
/**
* Specify a path to a required file
* @param {string} path
* @return {void}
*/
this.addFile = function(path)
{
files.push(path);
if (hasLoaded) this.includeFiles();
}
/**
* Load all files specified by addFile()
* @return {void}
*/
this.includeFiles = function()
{
for (var i in files)
{
if (typeof active[i] != 'undefined') continue;
var docHead = document.getElementsByTagName('head').item(0);
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = files[i];
docHead.appendChild(newScript);
active[i] = files[i];
}
hasLoaded = true;
}
//Load all files once this has run
window.onload = this.includeFiles;
}
//Prepare an instance for later use
var loader = new Loader();
Code: Select all
/**
* @fileoverview RPC fetches the required files for the SHANE JS API
* @author Chris Corbyn
*/
//loader is in the Loader.js file and is already instantiated
loader.addFile('sys/core/client/Registry.js');
loader.addFile('sys/core/client/MD5.js');
loader.addFile('sys/core/client/AuthResponse.js');
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia