Javascript Loader





11
Date Submitted Mon. Nov. 6th, 2006 8:33 AM
Revision 1 of 1
Scripter Casper42
Tags conditional | JavaScript | loader
Comments 0 comments
This script will allow for conditional loading of javascript libraries. The example here only has one condition to check for IE or Firefox then loads the appropriate .js file.

This is part of an .aspx template for an educational portal product.

Usage for loading the script:


//  Core functions for Blackbird Template
//  written by Jeremy Edmiston (jeremyedmiston@pointloma.edu)
//  The functions have been adapted from various sources
//  and re-written to provide maximum flexibility
//  and compatability with various browsers.

//Global Declarations
var ie = (document.all) ? true : false;
var scriptPath = templatePath + 'scripts/';
var stylePath = templatePath + 'styles/';
var imagePath = templatePath + 'images/';

//  Detect Browser and load appropriate scripts
if(ie){loadJS('ie.js')}else{loadJS('ff.js')}

function loadJS(scriptName) {
  var head = document.getElementsByTagName('head').item(0);
  var js = document.createElement('script');   
  js.setAttribute('language','javascript');
  js.setAttribute('type','text/javascript');
  if (scriptName.indexOf("/")== -1) {
    js.setAttribute('src', scriptPath + scriptName);
  }else{
    js.setAttribute('src', scriptName);
  }
  js.setAttribute('id',scriptName);
  js.setAttribute('defer','true');
  if (ie){
    document.write('<script language=' + js.language + ' type=' + js.type + ' src=' + js.src + ' id=' + js.id + ' ></script>');
  }else{
    head.appendChild(js)
  }
}

 

Jeremy Edmiston

Comments

There are currently no comments for this snippet.

Voting