Handle jQuery requests when you want to reference code that hasn't loaded yet
As you should be aware it is best practise to load your JavaScripts at the bottom of your HTML for performance and to stop blocking or slow load times.However sometimes you may want to reference an object that is not yet loaded higher up in the page.
If you are using a CMS or code that you cannot change then you may not be able to add your event handlers below any scripts that maybe needed to use them. This can cause errors such as:
Uncaught ReferenceError: $ is not defined
Uncaught ReferenceError: jQuery is not defined
If you cannot move your code below where the script is loaded then you can make use of a little PageLoader function that you can pass any functions to and which will hold them until jQuery (or any other object) is loaded before running the functions.
A simple implementation of this would involve a setTimeout call that constantly polls the check function until your script has loaded.
For example:
PageLoader = {
// holds the callback function to run once jQuery has loaded if you are loading jQuery in the footer and your code is above
jQueryOnLoad : function(){},
// call this function with your onload function as the parameter
CheckJQuery : function(func){
var f = false;
// has jQuery loaded yet?
if(window.jQuery){
f=true;
}
// if not we store the function if first time in loop otherwise and set a timeout
if(!f){
// if we have a function store it until jQuery has loaded
if(typeof(func)=="function"){
PageLoader.jQueryOnLoad = func;
}
// keep looping until jQuery is in the DOM
setTimeout(PageLoader.CheckJQuery,200);
}else{
// jQuery has loaded so call the function
PageLoader.jQueryOnLoad.call();
}
}
}
As you can see the object just holds the function passed to it in memory until jQuery has been loaded in the DOM. This will be apparent because window.jQuery will be true.
If the object isn't in the DOM yet then it just uses a setTimeout call to poll the function until it has loaded.
You could increase the length of time between the polls or even have a maximum limit so that it doesn't poll for ever and instead after 10 loops returns an error to the console. However this is just a simple example.
You would call the function by passing your jQuery referencing function to the CheckJQuery function like so.
<script>
PageLoader.CheckJQuery(function(){
$("#myelement").bind('click', function(e) {
// some code
alert("hello");
});
});
</script>
It's just a simple way to overcome a common problem where you cannot move your code about due to system limitations but require access to an object that will be loaded later on.
No comments:
Post a Comment