Last active
August 29, 2015 14:00
-
-
Save danbrianwhite/e3a9c94b6feeddc91b84 to your computer and use it in GitHub Desktop.
LazyLoad JS and CSS without Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _numFilesLazyLoad; | |
var _loadedFilesLazyLoad; | |
var _lazyLoaded = true; | |
var lazyLoadFiles = function(fileList, noCache) | |
{ | |
if(_lazyLoaded) | |
{ | |
_lazyLoaded = false; | |
_numFilesLazyLoad = 0; | |
_loadedFilesLazyLoad = 0; | |
var extensionExtractor = /\.[0-9a-z]+$/i; | |
/* count files */ | |
for (var i = 0; i < fileList.length; i++) | |
{ | |
var _fileName = fileList[i].fileName; | |
var _extension = _fileName.match(extensionExtractor)[0].toLowerCase(); | |
if(_extension === '.js') | |
{ | |
_numFilesLazyLoad++; | |
} | |
else if(_extension === '.css') | |
{ | |
_numFilesLazyLoad++; | |
} | |
}; | |
/* now lazy load them */ | |
for (var i = 0; i < fileList.length; i++) | |
{ | |
var _fileName = fileList[i].fileName; | |
var _extension = _fileName.match(extensionExtractor)[0].toLowerCase(); | |
if(_extension === '.js') | |
{ | |
lazyLoadJS(_fileName, noCache); | |
} | |
else if(_extension === '.css') | |
{ | |
lazyLoadCSS(_fileName, fileList[i].loaderID, noCache); | |
} | |
}; | |
} | |
} | |
var lazyLoadCSS = function(fileName, id, noCache) | |
{ | |
if(noCache === true) | |
{ | |
fileName = fileName + '?' + new Date().getTime(); | |
} | |
var _element = $('<div class="cssLazyLoader" id="'+id+'"></div>'); | |
$('body').append(_element) | |
$('head').append($('<link rel="stylesheet" type="text/css" href="'+fileName+'">')); | |
var _interval = setInterval(function(){ | |
var _loaded = _element.css('font-size'); | |
if(_loaded === '1px') | |
{ | |
clearInterval(_interval); | |
lazyLoaded(); | |
} | |
} , 50) | |
} | |
var lazyLoadJS = function(fileName, noCache) | |
{ | |
if(noCache === true) | |
{ | |
fileName = fileName + '?' + new Date().getTime(); | |
} | |
var scriptTag = document.createElement('script'); | |
scriptTag.setAttribute("type","text/javascript"); | |
scriptTag.setAttribute("src", fileName); | |
document.getElementsByTagName("head")[0].appendChild(scriptTag); | |
} | |
var lazyLoaded = function() | |
{ | |
_loadedFilesLazyLoad++; | |
checkLazyLoadComplete(); | |
} | |
var checkLazyLoadComplete = function() | |
{ | |
if(_loadedFilesLazyLoad === _numFilesLazyLoad) | |
{ | |
_lazyLoaded = true; | |
alert('loaded files'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
div.cssLazyLoader | |
{ | |
display: none; | |
font-size: 0px !important; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body | |
{ | |
background: red; | |
} | |
/* required for CSS LazyLoader */ | |
div#css_unique_id_for_styles_filename.cssLazyLoader | |
{ | |
font-size: 1px !important; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* required for JS LazyLoader */ | |
lazyLoaded(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazyLoadFiles([{fileName: 'test.js'}, {fileName: 'styles.css', loaderID: 'css_unique_id_for_styles_filename'}], true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment