Javascript的后加载,很多人都写过,这是为了防止最初加载的JS过多影响网速 。看到风雪之隅写了一个,看上去挺漂亮就转载了一下。。。
原文:http://www.laruence.com/2009/05/21/829.html
示例:
JavaScript代码
- var loader = Loader;
- loader.onReady = function(){
- alert('全部载入完成');
- }
- loader.on('onLoad', function(name) {
- alert(name + '载入完成');
- });
- loader.load([
- 'http://www.laruence.com/1.js',
- 'http://www.laruence.com/test.js',
- 'http://www.laruence.com/main.js'
- ]
- );
源码:
JavaScript代码
- /**
- * Eve Js Loader
- * @version 1.0.1
- * @author laruence<laruence at yahoo.com.cn>
- * @copyright (c) 2009 www.laruence.com
- */
- /**
- * a global object instance, you can easily change to a class definition
- */
- var Loader = {
- /**
- * @var onLoad when load a individual file completed ,
- * this event will be fired * @param string name loaded script file name
- */
- onLoad : function(name){},
- /**
- * @var onReady when all scripts loaded, this event will be fired
- */
- onReady : function(){},
- /**
- * a empty constructor
- */
- init : function(container) {
- },
- /**
- * a empty error handler
- */
- handlerError : function(e) {
- alert(e);
- },
- /**
- * event register
- * @param string evt event name
- * @param function handler
- */
- on : function(evt, handler) {
- switch ( evt.toLowerCase() ) {
- case 'load' :
- this.onLoad = handler;
- break;
- case 'ready' :
- this.onReady = handler;
- break;
- default :
- break;
- }
- return true;
- },
- /**
- * private method
- */
- _load : function(path, callback) {
- try {
- var script = document.createElement('script');
- script.src = path;
- script.type = "text/javascript";
- document.getElementsByTagName("head")[0].appendChild(script);
- if( script.addEventListener ) {
- script.addEventListener("load", callback, false);
- } else if(script.attachEvent) {
- script.attachEvent("onreadystatechange", function(){
- if(script.readyState == 4
- || script.readyState == 'complete'
- || script.readyState == 'loaded') {
- callback();
- }
- });
- }
- } catch(e) {
- this.handlerError(e);
- callback();
- }
- },
- /**
- * start loding process
- * @param array scripts files want to be loaded
- */
- load : function(scripts) {
- var total = scripts.length;
- var _self = this;
- var indicator = arguments[1] || 0;
- if ( indicator >= total ) {
- _self.onReady();
- return true;
- }
- var callee = arguments.callee;
- var script = scripts[indicator];
- this._load(script, function() {
- _self.onLoad(script);
- callee.apply(_self, [scripts, ++indicator]);
- });
- return true;
- }
- }
说到这里,其实,又让我想起了code.google.com提供的googleapi的加载方式,他提供了一些常用JS类库的加载,首先,它加载了googleapi:http://www.google.com/jsapi,然后就可以调用了
XML/HTML代码
- <script src="http://www.google.com/jsapi"></script>
- <script>
- // Load jQuery
- google.load("jquery", "1");
- // on page load complete, fire off a jQuery json-p query
- // against Google web search
- google.setOnLoadCallback(function() {
- $.getJSON("http://ajax.googleapis.com/ajax/services/search/web?q=google&v=1.0&callback=?",
- // on search completion, process the results
- function (data) {
- if (data.responseData.results &&
- data.responseData.results.length > 0) {
- var results = data.responseData.results;
- for (var i=0; i < results.length; i++) {
- // Display each result however you wish
- alert(results[i].titleNoFormatting);
- }
- }
- });
- });
- </script>
方式和原理差不多。仅作参考吧。。。