1+ /*
2+ * jQuery Late Loader
3+ *
4+ * Copyright (c) 2009 Erik Zaadi
5+ *
6+ * Plugin home page : http://plugins.jquery.com/project/TODO!
7+ * Wiki : http://wiki.github.com/erikzaadi/jQueryPlugins/jquerylateloader
8+ *
9+ * Dual licensed under the MIT and GPL licenses:
10+ * http://www.opensource.org/licenses/mit-license.php
11+ * http://www.gnu.org/licenses/gpl.html
12+ */
13+ ; ( function ( $ ) { $ . LateLoader = { LoadScriptOrCSS :_LoadScriptOrCSS , IsScriptOrCSSLoaded :_IsScriptOrCSSLoaded , GetLoadedScriptOrCSSs :_GetScriptOrCSSArray , GetLoadedScriptOrCSSsByType :_GetScriptOrCSSArrayByType , PluginDefaults :{ ArrayDataKey :"LateLoaderDataKey" , ElementToAttachDataTo :"body" , RemoteTimeout :1500 } , Defaults :{ URL :null , Type :"js" , LoadedCallBackFunction :null , ErrorCallBackFunction :null } } ;
14+ function _LoadScriptOrCSS ( OptionsOrURL ) { var options ; if ( typeof ( OptionsOrURL ) == "string" ) { options = $ . extend ( { } , $ . LateLoader . Defaults , { URL :OptionsOrURL } ) ;
15+ } else { options = $ . extend ( { } , $ . LateLoader . Defaults , OptionsOrURL ) ; } if ( _IsScriptOrCSSLoaded ( options . URL , options . Type ) ) { _CallFunctionIfAvailable ( options . LoadedCallBackFunction , options . URL ) ;
16+ return ; } if ( options . Type == "js" ) { if ( _URLIsRemote ( options . URL ) ) { setTimeout ( function ( ) { if ( ! _IsScriptOrCSSLoaded ( options . URL , options . Type ) ) { _Error ( options ) ;
17+ } } , $ . LateLoader . PluginDefaults . RemoteTimeout ) ; } $ . ajax ( { dataType :"script" , url :options . URL , success :function ( ) { _Success ( options ) ;
18+ } , error :function ( ) { _Error ( options ) ; } , data :{ } } ) ; } else { _AddCSSFile ( options ) ; } } function _IsScriptOrCSSLoaded ( URL , Type ) { var ScriptOrCSSArray = _GetScriptOrCSSArray ( ) || { } ;
19+ return ( ScriptOrCSSArray [ Type ] && ( $ . inArray ( URL , ScriptOrCSSArray [ Type ] ) != - 1 ) ) ?true :false ;
20+ } function _GetScriptOrCSSArray ( ) { var ScriptOrCSSData = $ ( $ . LateLoader . PluginDefaults . ElementToAttachDataTo ) . data ( $ . LateLoader . PluginDefaults . ArrayDataKey ) ;
21+ var ScriptOrCSSArray = ScriptOrCSSData && ScriptOrCSSData . ScriptOrCSSs ?ScriptOrCSSData . ScriptOrCSSs :false ;
22+ if ( ! ScriptOrCSSArray ) { return false ; } else { return ScriptOrCSSArray ; } } function _GetScriptOrCSSArrayByType ( Type ) { var ScriptOrCSSArray = _GetScriptOrCSSArray ( ) || false ;
23+ if ( ! ScriptOrCSSArray ) { return false ; } if ( ! ScriptOrCSSArray [ Type ] ) { return false ; } return ScriptOrCSSArray [ Type ] ;
24+ } function _Error ( options ) { _CallFunctionIfAvailable ( options . ErrorCallBackFunction , "error loading " + options . Type + " - " + options . URL ) ;
25+ } function _Success ( options ) { _AddLoadedScriptOrCSSToArray ( options ) ; _CallFunctionIfAvailable ( options . LoadedCallBackFunction , options . URL ) ;
26+ } function _ValidateCSSFileLoaded ( options ) { var $createdLink = $ ( "link[href='" + options . URL + "']" ) ;
27+ var success = true ; if ( ! $createdLink . length ) { success = false ; } if ( $createdLink . get ( 0 ) . readyState ) { var created = $createdLink . get ( 0 ) . readyState ;
28+ success = created == "complete" || created == "loaded" ; } else { if ( false ) { success = false ; } } if ( success ) { _Success ( options ) ;
29+ } else { _Error ( options ) ; } } function _AddCSSFile ( options ) { if ( ! _URLIsRemote ( options . URL ) ) { $ . ajax ( { url :options . URL , dataType :"text" , success :function ( data ) { var randID = Math . round ( Math . random ( ) * 321312 ) ;
30+ var $link = $ ( "<style />" ) . attr ( { rel :"stylesheet" , type :"text/css" , id :randID . toString ( ) } ) ;
31+ $link . appendTo ( "head" ) ; setTimeout ( function ( ) { var created = $ ( "#" + randID . toString ( ) ) ;
32+ if ( $ . browser . msie ) { created . get ( 0 ) . styleSheet . cssText = data ; } else { created . text ( data ) ;
33+ } _Success ( options ) ; } , 15 ) ; } , error :function ( ) { _Error ( options ) ; } , data :{ } } ) ; } else { $ ( "<link />" ) . ready ( function ( ) { setTimeout ( function ( ) { _ValidateCSSFileLoaded ( options ) ;
34+ } , 15 ) ; } ) . attr ( { rel :"stylesheet" , type :"text/css" , href :options . URL } ) . appendTo ( "head" ) ;
35+ } } function _AddLoadedScriptOrCSSToArray ( options ) { var ScriptOrCSSArray = _GetScriptOrCSSArray ( ) || { } ;
36+ if ( ! ScriptOrCSSArray [ options . Type ] ) { ScriptOrCSSArray [ options . Type ] = new Array ( ) ; } ScriptOrCSSArray [ options . Type ] . push ( options . URL ) ;
37+ $ ( $ . LateLoader . PluginDefaults . ElementToAttachDataTo ) . data ( $ . LateLoader . PluginDefaults . ArrayDataKey , { ScriptOrCSSs :ScriptOrCSSArray } ) ;
38+ } function _CallFunctionIfAvailable ( method , param ) { if ( method && $ . isFunction ( method ) ) { method ( param ) ;
39+ } } function _URLIsRemote ( url ) { return url . indexOf ( "http" ) > - 1 && ( url . indexOf ( "http://" + window . location . host ) == - 1 && url . indexOf ( "https://" + window . location . host ) == - 1 ) ;
40+ } } ) ( jQuery ) ;
0 commit comments