- var LinkFile = Class.create();
- LinkFile.prototype = {initialize: function (_url, options) {
- this .options = Object.extend({type: "script" , charset: "" , noCache: false , callBack: null }, options || {});
- this .options.callBack = Object.extend({variable: null , onLoad: null , timeout:20000, timerStep:500}, options.callBack || {});
- this .timer = 0;
- this .loadTimer = null ;
- if ( this .options.type == "script" ) {
- //this.getJs(_url.trim());
- this .getJs(_url);
- } else {
- //this.getCss(_url.trim());
- this .getCss(_url);
- }
- if ( this .options.callBack.variable) {
- this .options.callBack.vars = this .options.callBack.variable.split( "." );
- }
- }, stop: function () {
- clearInterval( this .loadTimer);
- this .loadTimer = null ;
- this .timer = 0;
- return ;
- }, doCallback: function () {
- if ( this .options.type != "script" || ! this .options.callBack || ! this .options.callBack.vars || ! this .options.callBack.onLoad) {
- this .stop();
- return ;
- }
- this .timer += this .options.callBack.timerStep;
- if ($A( this .options.callBack.vars).any(
- function (v, i) {
- var _v = this .options.callBack.vars.slice(0, i + 1).join( "." );
- return (eval( "typeof " + _v + "== /"undefined/"" ) || eval(_v + "==null" ));
- }.bind( this )) && ( this .timer < this .options.callBack.timeout)) {
- return ;
- } else {
- clearInterval( this .loadTimer);
- if ($A( this .options.callBack.vars).all( function (v, i) {
- var _v = this .options.callBack.vars.slice(0, i + 1).join( "." );
- return (eval( "typeof " + _v + "!= /"undefined/"" ) && eval(_v + "!=null" ));
- }.bind( this ))) {
- ( this .options.callBack.onLoad)();
- } else {
- if (( this .timer >= this .options.callBack.timeout) && this .options.callBack.onFailure) {
- ( this .options.callBack.onFailure)();
- }
- }
- this .loadTimer = null ;
- this .timer = 0;
- }
- }, getJs: function (_url) {
- var oHead = document.getElementsByTagName( "head" )[0];
- var _links = Element.getChildElementByTagName(oHead, "SCRIPT" );
- $A(_links).each( function (s) {
- if (getFullUrl(s.getAttribute( "src" ) || "" ) == getFullUrl(_url)) {
- Element.remove(s);
- }
- });
- this ._link = document.createElement( "script" );
- if ( this .options.noCache) {
- _url += (_url.match(//?/) ? "&" : "?" ) + "c=" + timeStamp();
- }
- this ._link.src = _url;
- this ._link.type = "text/javascript" ;
- if ( this .options.charset) {
- this ._link.charset = this .options.charset;
- }
- if ( this .options.callBack) {
- this .loadTimer = setInterval( function () {
- this .doCallback();
- }.bind( this ), this .options.callBack.timerStep);
- }
- oHead.appendChild( this ._link);
- }, getCss: function (_url) {
- var oHead = document.getElementsByTagName( "head" )[0];
- var _links = Element.getChildElementByTagName(oHead, "LINK" );
- $A(_links).each( function (l) {
- if (getFullUrl(l.getAttribute( "href" ) || "" ) == getFullUrl(_url)) {
- Element.remove(l);
- }
- });
- this ._link = document.createElement( "link" );
- if ( this .options.noCache) {
- _url += (_url.match(//?/) ? "&" : "?" ) + "c=" + timeStamp();
- }
- this ._link.href = _url;
- this ._link.type = "text/css" ;
- this ._link.rel = "stylesheet" ;
- if ( this .options.charset) {
- this ._link.charset = this .options.charset;
- }
- oHead.appendChild( this ._link);
- }};
使用方法如下:
1 ===========
function search(){
new LinkFile(url, {
type: 'script',
callBack: {
variable: 'search_result',
onLoad: show_result.bind(this),
onFailure: function(){},
timeout: 50000
/*timerStep: 500*/
}});
}
2 ============
function showresult(){
new LinkFile(commentjspath, {type: 'script',callBack: {variable: 'comments',
onLoad: function(){loadJS(commentjspath);showComments(6,1, 6 ,"",<%=articleid%> );},
onFailure: function(){document.getElementById("updatecontent").innerHTML=""; $("commentCount").innerHTML = "("+0+")"; },
timeout: 10000}});
}
基于prototype的动态加载js的一个类
最新推荐文章于 2022-09-02 21:03:23 发布