Skip to content

Instantly share code, notes, and snippets.

@yoko
Created November 17, 2008 02:30
Show Gist options
  • Save yoko/25638 to your computer and use it in GitHub Desktop.
Save yoko/25638 to your computer and use it in GitHub Desktop.
ライブラリ動的読み込み
new function() {
var callback = function() {
// main code
};
// util
var script = (function() {
var s = document.getElementsByTagName('script');
return s[s.length - 1];
})();
var swfobject, $; // libs' namespase
new function() {
var lib = [
{
src : 'http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js',
complete: (function() {
var _swfobject = window.swfobject || undefined;
return function() {
swfobject = window.swfobject;
window.swfobject = _swfobject;
};
})()
},
{
src : 'http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js',
complete: function() {
$ = window.jQuery.noConflict(true);
// plugins
// http://gist.github.com/14048
$.fn.fl = function(o) {
if (!o) {
var id = this.attr('id');
return $.browser.msie ? window[id] : document[id];
}
o = $.extend({
version: '9.0.47',
params : {
wmode: 'transparent'
}
}, o);
if (swfobject.hasFlashPlayerVersion(o.version)) {
o.attrs = $.extend(o.attrs || {}, {
data : o.src,
width : o.width,
height: o.height
});
if (o.flashvars) o.params.flashvars = $.param(o.flashvars);
var element = o.id ?
$('<div id="'+o.id+'"/>').appendTo(this) :
this;
swfobject.createSWF(
o.attrs,
o.params,
element.attr('id')
);
}
return this;
};
}
}
];
var load = function() {
var f = arguments.callee;
var l = lib.shift();
if (!l || findScript(l.src) != -1) {
callback();
return;
}
if (l.beforeSend) beforeSend();
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = l.src;
script.charset = 'UTF-8';
var done = false;
script.onload = script.onreadystatechange = function() {
if (
!done && (!this.readyState ||
this.readyState == 'loaded' || this.readyState == 'complete')
) {
done = true;
if (l.complete) l.complete();
f();
}
};
document.body.appendChild(script);
function findScript(src) {
var s = document.getElementsByTagName('script');
var i = s.length;
while (i--) if (s[i].src == src) return i;
return -1;
}
};
window.attachEvent ? window.attachEvent('onload', load) : load();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment