Skip to main content
    function jsLoader()
    {
    var o = this;
    
    o.timer = function(t,i,d,f,fend,b) // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    o.timer = function(t, i, d, f, fend, b)
    {
        if( t == -1 || t > 0 )
        {
            setTimeout( function() {
                b=(f()) ? 1 : 0;
                o.timer( (b) ? 0 : (t>0) ? --t : t, i+((d) ? d : 0), d, f, fend,b );
            }, (b||i<0b || i < 0) ? 0.1 :i i);
        }
        else if( typeof fend == 'function' )
        {
    {        setTimeout( fend, 1 );
        }
    };
    
    o.addEvent = function( el, eventName, eventFunc )
    {
        if( typeof el != 'object' )
        {
            return false;
        }

        if( el.addEventListener )
        {
            el.addEventListener ( eventName, eventFunc, false);
            return true;
        }

        if( el.attachEvent )
        {
            el.attachEvent("on"+eventName"on" + eventName, eventFunc );
            return true;
        }

        return false;
    };
    
    // add script to dom
    o.require = function( s, delay, baSync, fCallback, fErr ) // 
 add script to dom{
    {
     var oo = document.createElement('script'),
        oHead = document.getElementsByTagName('head')[0];
        if( !oHead )
        {
            return false;
        }
    
        setTimeout( function() {
            var f = (typeof fCallback == 'function') ? fCallback : function(){};
            fErr = (typeof fErr == 'function') ? fErr : function(){
                alert('require: Cannot load resource -'+s);
            },
            fe = function(){
                if(!oo.__es)
                {
                    oo.__es=true;__es = true;
                    oo.id='failed';id = 'failed';
                    fErr(oo);
                }
            };
            oo.onload = function() {
                oo.id='loaded';id = 'loaded';
                f(oo);
            };
            oo.type = 'text/javascript';
            oo.async = (typeof baSync == 'boolean') ? baSync : false;
            oo.charset='utfcharset = 'utf-8';
            o.__es = false;
            o.addEvent( oo, 'error', fe ); // when supported
            
            // when error event is not supported fall back to timer
            o.timer( 15, 1000, 0, function() {
                return (oo.id == 'loaded');
            }, function(){ 
                if(oo.id != 'loaded'){
                    fe();
                }
            });
            oo.src = s;
            setTimeout( function() {
                try{
                    oHead.appendChild(oo);
                }catch(e){
                    fe();
                }
            },1); 
        }, (typeof delay == 'number') ? delay :1 1);  
        return true;
    };
    

}

}
     
$(document).ready( function()
    {
     var ol = new jsLoader();
     ol.require('myscript.js', 800, true, function() {
        alert('loaded');
    }, function() {alert('NOT 
 loaded');});   
     alert('NOT loaded');
    });
});
    function jsLoader()
    {
    var o = this;
    
    o.timer = function(t,i,d,f,fend,b) // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    {
      if( t == -1 || t > 0 )
       { setTimeout( function() { b=(f())?1:0; o.timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
      else if( typeof fend == 'function' )
            { setTimeout( fend, 1 ); }
    };
    
o.addEvent = function( el, eventName, eventFunc )
{
 if( typeof el != 'object' )
 { return false; }

 if( el.addEventListener )
 {
   el.addEventListener ( eventName, eventFunc,false);
   return true;
 }

 if( el.attachEvent )
 {
  el.attachEvent("on"+eventName, eventFunc );
  return true;
 }

 return false;
};
    
    
    o.require = function( s, delay, baSync, fCallback, fErr ) // add script to dom
    {
     var oo = document.createElement('script'),
     oHead = document.getElementsByTagName('head')[0];
     if( !oHead )
      { return false; }
    
     setTimeout( function() {
     var f = (typeof fCallback == 'function')?fCallback:function(){};
     fErr = (typeof fErr == 'function')?fErr:function(){alert('require: Cannot load resource -'+s);},
     fe = function(){ if(!oo.__es){oo.__es=true; oo.id='failed'; fErr(oo);}};
     oo.onload = function() {oo.id='loaded'; f(oo); };
     oo.type = 'text/javascript';
     oo.async = (typeof baSync == 'boolean')?baSync:false;
     oo.charset='utf-8';
     o.__es = false;
     o.addEvent( oo, 'error', fe ); // when supported
     // when error event is not supported fall back to timer
     o.timer( 15, 1000, 0, function() {return (oo.id == 'loaded');}, function(){ if(oo.id != 'loaded'){fe();}});
     oo.src = s;
     setTimeout( function() { try{ oHead.appendChild(oo);}catch(e){fe();}},1); 
     }, (typeof delay == 'number')?delay:1 );  
     return true;
    };
    

}

    $(document).ready( function()
    {
     var ol = new jsLoader();
     ol.require('myscript.js',800,true, function() {alert('loaded');},function() {alert('NOT loaded');});   
     
    });
function jsLoader()
{
    var o = this;

    // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    o.timer = function(t, i, d, f, fend, b)
    {
        if( t == -1 || t > 0 )
        {
            setTimeout(function() {
                b=(f()) ? 1 : 0;
                o.timer((b) ? 0 : (t>0) ? --t : t, i+((d) ? d : 0), d, f, fend,b );
            }, (b || i < 0) ? 0.1 : i);
        }
        else if(typeof fend == 'function')
        {
            setTimeout(fend, 1);
        }
    };
    
    o.addEvent = function(el, eventName, eventFunc)
    {
        if(typeof el != 'object')
        {
            return false;
        }

        if(el.addEventListener)
        {
            el.addEventListener (eventName, eventFunc, false);
            return true;
        }

        if(el.attachEvent)
        {
            el.attachEvent("on" + eventName, eventFunc);
            return true;
        }

        return false;
    };
    
    // add script to dom
    o.require = function(s, delay, baSync, fCallback, fErr) 
    {
        var oo = document.createElement('script'),
        oHead = document.getElementsByTagName('head')[0];
        if(!oHead)
        {
            return false;
        }
    
        setTimeout( function() {
            var f = (typeof fCallback == 'function') ? fCallback : function(){};
            fErr = (typeof fErr == 'function') ? fErr : function(){
                alert('require: Cannot load resource -'+s);
            },
            fe = function(){
                if(!oo.__es)
                {
                    oo.__es = true;
                    oo.id = 'failed';
                    fErr(oo);
                }
            };
            oo.onload = function() {
                oo.id = 'loaded';
                f(oo);
            };
            oo.type = 'text/javascript';
            oo.async = (typeof baSync == 'boolean') ? baSync : false;
            oo.charset = 'utf-8';
            o.__es = false;
            o.addEvent( oo, 'error', fe ); // when supported
            
            // when error event is not supported fall back to timer
            o.timer(15, 1000, 0, function() {
                return (oo.id == 'loaded');
            }, function(){ 
                if(oo.id != 'loaded'){
                    fe();
                }
            });
            oo.src = s;
            setTimeout(function() {
                try{
                    oHead.appendChild(oo);
                }catch(e){
                    fe();
                }
            },1); 
        }, (typeof delay == 'number') ? delay : 1);  
        return true;
    };
    
}
     
$(document).ready( function()
{
    var ol = new jsLoader();
    ol.require('myscript.js', 800, true, function(){
        alert('loaded');
    }, function() { 
        alert('NOT loaded');
    });
});
remove wrong end of statement - };
Source Link
Codebeat
  • 6.5k
  • 6
  • 60
  • 102
    function jsLoader()
    {
    var o = this;
    
    o.timer = function(t,i,d,f,fend,b) // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    {
      if( t == -1 || t > 0 )
       { setTimeout( function() { b=(f())?1:0; o.timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
      else if( typeof fend == 'function' )
            { setTimeout( fend, 1 ); }
    };
    
o.addEvent = function( el, eventName, eventFunc )
{
 if( typeof el != 'object' )
 { return false; }

 if( el.addEventListener )
 {
   el.addEventListener ( eventName, eventFunc,false);
   return true;
 }

 if( el.attachEvent )
 {
  el.attachEvent("on"+eventName, eventFunc );
  return true;
 }

 return false;
};
    };
    
    o.require = function( s, delay, baSync, fCallback, fErr ) // add script to dom
    {
     var oo = document.createElement('script'),
     oHead = document.getElementsByTagName('head')[0];
     if( !oHead )
      { return false; }
    
     setTimeout( function() {
     var f = (typeof fCallback == 'function')?fCallback:function(){};
     fErr = (typeof fErr == 'function')?fErr:function(){alert('require: Cannot load resource -'+s);},
     fe = function(){ if(!oo.__es){oo.__es=true; oo.id='failed'; fErr(oo);}};
     oo.onload = function() {oo.id='loaded'; f(oo); };
     oo.type = 'text/javascript';
     oo.async = (typeof baSync == 'boolean')?baSync:false;
     oo.charset='utf-8';
     o.__es = false;
     o.addEvent( oo, 'error', fe ); // when supported
     // when error event is not supported fall back to timer
     o.timer( 15, 1000, 0, function() {return (oo.id == 'loaded');}, function(){ if(oo.id != 'loaded'){fe();}});
     oo.src = s;
     setTimeout( function() { try{ oHead.appendChild(oo);}catch(e){fe();}},1); 
     }, (typeof delay == 'number')?delay:1 );  
     return true;
    };
    
    }
    

}

    $(document).ready( function()
    {
     var ol = new jsLoader();
     ol.require('myscript.js',800,true, function() {alert('loaded');},function() {alert('NOT loaded');});   
    
    });
    function jsLoader()
    {
    var o = this;
    
    o.timer = function(t,i,d,f,fend,b) // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    {
      if( t == -1 || t > 0 )
       { setTimeout( function() { b=(f())?1:0; o.timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
      else if( typeof fend == 'function' )
            { setTimeout( fend, 1 ); }
    };
    
o.addEvent = function( el, eventName, eventFunc )
{
 if( typeof el != 'object' )
 { return false; }

 if( el.addEventListener )
 {
   el.addEventListener ( eventName, eventFunc,false);
   return true;
 }

 if( el.attachEvent )
 {
  el.attachEvent("on"+eventName, eventFunc );
  return true;
 }

 return false;
};
    };
    
    o.require = function( s, delay, baSync, fCallback, fErr ) // add script to dom
    {
     var oo = document.createElement('script'),
     oHead = document.getElementsByTagName('head')[0];
     if( !oHead )
      { return false; }
    
     setTimeout( function() {
     var f = (typeof fCallback == 'function')?fCallback:function(){};
     fErr = (typeof fErr == 'function')?fErr:function(){alert('require: Cannot load resource -'+s);},
     fe = function(){ if(!oo.__es){oo.__es=true; oo.id='failed'; fErr(oo);}};
     oo.onload = function() {oo.id='loaded'; f(oo); };
     oo.type = 'text/javascript';
     oo.async = (typeof baSync == 'boolean')?baSync:false;
     oo.charset='utf-8';
     o.__es = false;
     o.addEvent( oo, 'error', fe ); // when supported
     // when error event is not supported fall back to timer
     o.timer( 15, 1000, 0, function() {return (oo.id == 'loaded');}, function(){ if(oo.id != 'loaded'){fe();}});
     oo.src = s;
     setTimeout( function() { try{ oHead.appendChild(oo);}catch(e){fe();}},1); 
     }, (typeof delay == 'number')?delay:1 );  
     return true;
    };
    
    }
    
    $(document).ready( function()
    {
     var ol = new jsLoader();
     ol.require('myscript.js',800,true, function() {alert('loaded');},function() {alert('NOT loaded');});   
    
    });
    function jsLoader()
    {
    var o = this;
    
    o.timer = function(t,i,d,f,fend,b) // simple unstopable repeat timer, when t=-1 means endless, when function f() returns true it can be stopped
    {
      if( t == -1 || t > 0 )
       { setTimeout( function() { b=(f())?1:0; o.timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
      else if( typeof fend == 'function' )
            { setTimeout( fend, 1 ); }
    };
    
o.addEvent = function( el, eventName, eventFunc )
{
 if( typeof el != 'object' )
 { return false; }

 if( el.addEventListener )
 {
   el.addEventListener ( eventName, eventFunc,false);
   return true;
 }

 if( el.attachEvent )
 {
  el.attachEvent("on"+eventName, eventFunc );
  return true;
 }

 return false;
};
    
    
    o.require = function( s, delay, baSync, fCallback, fErr ) // add script to dom
    {
     var oo = document.createElement('script'),
     oHead = document.getElementsByTagName('head')[0];
     if( !oHead )
      { return false; }
    
     setTimeout( function() {
     var f = (typeof fCallback == 'function')?fCallback:function(){};
     fErr = (typeof fErr == 'function')?fErr:function(){alert('require: Cannot load resource -'+s);},
     fe = function(){ if(!oo.__es){oo.__es=true; oo.id='failed'; fErr(oo);}};
     oo.onload = function() {oo.id='loaded'; f(oo); };
     oo.type = 'text/javascript';
     oo.async = (typeof baSync == 'boolean')?baSync:false;
     oo.charset='utf-8';
     o.__es = false;
     o.addEvent( oo, 'error', fe ); // when supported
     // when error event is not supported fall back to timer
     o.timer( 15, 1000, 0, function() {return (oo.id == 'loaded');}, function(){ if(oo.id != 'loaded'){fe();}});
     oo.src = s;
     setTimeout( function() { try{ oHead.appendChild(oo);}catch(e){fe();}},1); 
     }, (typeof delay == 'number')?delay:1 );  
     return true;
    };
    

}

    $(document).ready( function()
    {
     var ol = new jsLoader();
     ol.require('myscript.js',800,true, function() {alert('loaded');},function() {alert('NOT loaded');});   
    
    });
spelling, grammar, remove sig
Source Link
Andrew Marshall
  • 96.4k
  • 20
  • 223
  • 217

I know this is an old thread but iI got a nice solution to you (iI think). It's It's copied from an class of mine, that handles all ajaxAJAX stuff.

When the script cannot be loaded, it set an error handler but when the error handler is not supported, it falls back to a timer that checks for errors for 15 seconds.

The person that dig me with -1 is not right, it is not a bad solution!

Maybe it is useful to you! Enjoy! Greetz, Erwin Haantjes

I know this is an old thread but i got a nice solution to you (i think). It's copied from an class of mine, that handles all ajax stuff.

When the script cannot be loaded, it set an error handler but when the error handler is not supported, it falls back to a timer that checks for errors for 15 seconds.

The person that dig me with -1 is not right, it is not a bad solution!

Maybe it is useful to you! Enjoy! Greetz, Erwin Haantjes

I know this is an old thread but I got a nice solution to you (I think). It's copied from an class of mine, that handles all AJAX stuff.

When the script cannot be loaded, it set an error handler but when the error handler is not supported, it falls back to a timer that checks for errors for 15 seconds.

added 76 characters in body
Source Link
Codebeat
  • 6.5k
  • 6
  • 60
  • 102
Loading
deleted 140 characters in body
Source Link
Codebeat
  • 6.5k
  • 6
  • 60
  • 102
Loading
deleted 60 characters in body
Source Link
Codebeat
  • 6.5k
  • 6
  • 60
  • 102
Loading
Source Link
Codebeat
  • 6.5k
  • 6
  • 60
  • 102
Loading