/**
* Author: wade.fs@gmail.com
* Original author Dav Glass <dav.glass@yahoo.com>
* version 0.2 20060126
* desc: 本程式必須配合 YUI 使用
*/
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>YAHOO</TITLE>
<script type="text/javascript" src="js/yahoo.js"></script>
<script type="text/javascript" src="js/connection.js"></script>
<script type="text/javascript" src="js/loader.js"></script>
<script type="text/javascript">
var ob = {
'A': "a char A",
'Z': "char Z here",
};
function act(o, keyCode, status) {
if (keyCode == 65) { alert (o['A']); }
else { alert (o['Z']); }
}
function keyLoad() {
document.onkeydown = Key.pressed;
Key.addListener(act, ob, ""); // register any key for ob
}
function init() {
var myJsLoader = new YAHOO.Loader;
myJsLoader.onFileLoad = keyLoad;
var urls = [ "js/key.js" ];
myJsLoader.add(urls);
myJsLoader.load();
}
</script>
</HEAD>
<BODY id="ss-body" onLoad="init()">
</BODY>
</HTML>
*/
YAHOO.Loader = function() { this.init(); };
YAHOO.Loader.prototype = {
id: null,
scripts: {},
isLoaded: {},
holder: null,
init: function(id) {
this.holder = document.createElement('script');
this.holder.id = 'wadefs_loader_holder';
this.holder.type = 'text/javascript';
document.getElementsByTagName('body')[0].appendChild(this.holder);
},
onFileLoad: function(fileName, o) {},
onFileFail: function(fileName, o) {},
transActions: [],
add: function(file) {
if (file instanceof Array) {
for (var i = 0; i < file.length; i++) {
this.scripts[file[i]] = file[i];
}
} else {
this.scripts[file] = file;
}
},
insertScript: function(name) {
if (typeof(name) == "undefined") return;
if (!this.isLoaded[name]) {
var req = YAHOO.util.Connect.asyncRequest('GET', name, {
success: this.insertGood,
failure: this.insertBad,
caller: this,
});
this.transActions[req.tId] = name;
this.isLoaded[name] = name;
}
},
insertGood: function(o) {
if (o.responseText !== "undefined"){
var file = this.caller.transActions[o.tId];
if (file.substr(0,4) == "i18n") {
i18n.lang = eval('(' + o.responseText + ')');
}
else
this.caller.holder.innerHTML += o.responseText;
this.caller.onFileLoad(this.caller.transActions[o.tId], o);
}
},
insertBad: function(o) {
o.fileName = this.caller.transActions[o.tId];
this.caller.onFileFail(this.caller.transActions[o.tId], o);
},
load: function(thisFile) {
if (thisFile) {
if (thisFile instanceof Array) {
for (var i = 0; i < thisFile.length; i++) {
this.insertScript(thisFile[i]);
}
} else {
this.insertScript(thisFile);
}
} else {
for (var i in this.scripts) {
this.insertScript(this.scripts[i]);
}
}
},
};
2007/01/25
動態載入 javascript
在發展 AJAX 應用時常常會把 javascript 寫的太大了,分成好幾個檔當然是應該的,但是對載入卻無法加速,底下片斷是改來的,不過寫法差異很大,想要真正能用的話還得下載YUI才行。
訂閱:
張貼留言 (Atom)
0 意見:
張貼留言