Page 1 of 1

Javascript include

Posted: Fri Oct 13, 2006 12:27 pm
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...

Posted: Fri Oct 13, 2006 8:44 pm
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);

Re: Javascript include

Posted: Sat Oct 14, 2006 4:39 am
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');

Posted: Sat Oct 14, 2006 5:09 am
by n00b Saibot
jigging it the OOP way, eh baby :)

Posted: Sat Oct 14, 2006 10:56 am
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.