Include only the code you want to execute when the DOM is ready for this particular page. $(document).ready(... does not mean that every page should have the same piece of code. This would require different checks to know what needs to be done.
I am sure that you will have common functions that you want to perform on each page, as well as some functions related to specific pages. If so, you can put common functionality in one function and call it from $(document).ready(... so that the only remaining code is specific to that particular page. For example:
function common() { alert('hello'); } $(document).ready(function() { common();
I disagree with doing various checks on each page, so the code knows where we are. This seems cumbersome and completely preventable, for example:
function doAnimation() { $("#element_1").fadeIn(); $("#element_2").delay('100').fadeIn(); $("#element_3").delay('200').fadeIn(); } $(document).ready(function() { if(window.location.href == 'http://example.com/foo') { doAnimation(); } if(this page has blah) { doBlah(); } });
Ideally, this should be:
$(document).ready(function() {
karim79
source share