Javascript include

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Javascript include

Post by Luke »

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...
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Javascript include

Post by Chris Corbyn »

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...
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:

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');
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

jigging it the OOP way, eh baby :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

n00b Saibot wrote:jigging it the OOP way, eh baby :)
Naturally :) The only way; JS *is* by it's very nature an OO language through and through :)

I need the use a lot of OO anyway since the PHP framework I'm working on provides a JS framework/API too.
Post Reply