There is a fundamental difference there. That is is when the DOM is ready, which is usually ideal. This is when the whole page has finished loading, which is different. For example, this will wait for images to be finished loading, so that you can measure their widths and heights accurately while your snippet will not wait. Small but sometimes crucial difference.
it depends how much code you have and if you are erver going to reuse. If you want to reuse put function in the JS file with reference to a variable that lives in
Then you can put any id you want per page and if item of this id exists it will be processed.
so in JS file you dont hardcode stuff that much just check if varialbe is ther and what is the value.
Is there way to do this without jQuery? I use jQuery (but the script will be loaded at page-end). I’m looking for a JS-Snippet to use in the Content (that will be executed when the page has loaded.
Small thing. When using the button “copy to clipboard” it will copy it with curly ‘”‘ and not just straight “. With the curly ones the code doesn’t work…
I’m trying to load page before load the map ( I use is map extreme ) although I use document ready function, the page trying to load mapLabels with page objects. for a while user see my page as there is no css. After the map finished it’s job the css loaded. How can I fix this? I use your code but it didn’t work for me.
Here is an example;
j(window).bind(“load”, function () {
MapXtremeJavascriptLib.init();
});
/* DOCUMENT READY FUNCTION AREA START */
j(document).ready(function () {
j(“.accordion”).accordion({
autoHeight: true,
navigation: true,
collapsible: true
});
/* DOCUMENT READY FUNCTION AREA END*/
Hi all,
I’m battering my head about how I can A: Tell a javascript to wait till the entire site has loaded. I guess I just found the solution to that. And B: Have the javascript load only on the home page.
The site runs on joomla and before I had the script run from a custom html module which I had assigned to the debug position and set to only load on the home page.
That worked fine for firefox, chrome and safari but IE was mocking up (as usual).
Now I’m battering my head about how I can add it so that the script (a lady walking into the site and welcoming the visitors) will only show on the home page.
thanks, chris before knowing this I used setTimeout, and the bad thing using this, is we never exactly know how long the page load time on the user browser
Should work fine on IE9. At least it is for me. Doesn’t work on IE8 and below though.
What it doesn’t work on however if the current version of Firefox. Apparently firefox treats .load and .ready as the same and loads .load after the javascript has loaded and doesn’t wait for all images and other page content like the other browsers do. Does anyone know of a workaround for firefox?
Great… jQuery again. Looking at the title in Google, I thought you were going to present something non-framework. Bah, why do people ignore the fact that it’s not cool to load a 32KB jQuery lib for something that could be done in a few lines.
Thanks anyway… even when this didn’t help me at all.
You and your users probably have jQuery cached on their systems so I guess not an issue – plus if you use Google as the CDN its blindingly fast – so why not?
I want to Load my Minified JS after the page load.
Currently it takes 1.8 sec to 2.5 sec to load this JS File which is increasing my complete webpage load to 4.2 sec to 5.1 sec.
So how can i load this JS without delaying the page load.
As always, jump on google have a nosey and the first result from Css-Tricks is the winner on the day!, Cheers for your time invested in the web dev commnity Chris.
$(window).bind(“load”, function(){
});
The above function works fine only on the initial load. But when we navigate to differect page and come back to the same page. the function gets executed before complete loading of Dom elements. Kindly tell me if there is another way
This should go in jQuery
Moved.
A shorthand version of this is:
$(function() {// code here
}
There is a fundamental difference there. That is is when the DOM is ready, which is usually ideal. This is when the whole page has finished loading, which is different. For example, this will wait for images to be finished loading, so that you can measure their widths and heights accurately while your snippet will not wait. Small but sometimes crucial difference.
$(function() {
// code here
}
Your syntax is incorrect as well. It should be:
$(function() { // code here });Be sure to close those brackets and end with a semicolon.
This is probably my first comment on your site and I’ve been a fan and reading for a long while probably since podcast 20 XD
I haven’t been able to figure out where to place this code..?
If you can help me out, give me an example with your “simple jquery accordion” – http://css-tricks.com/snippets/jquery/simple-jquery-accordion/
Thanks, Chris!
Place the code in your <head>HERE</head> section.
it depends how much code you have and if you are erver going to reuse. If you want to reuse put function in the JS file with reference to a variable that lives in
Then you can put any id you want per page and if item of this id exists it will be processed.
so in JS file you dont hardcode stuff that much just check if varialbe is ther and what is the value.
for this example i guess it would be an overkill
:- )
Is there way to do this without jQuery? I use jQuery (but the script will be loaded at page-end). I’m looking for a JS-Snippet to use in the Content (that will be executed when the page has loaded.
Small thing. When using the button “copy to clipboard” it will copy it with curly ‘”‘ and not just straight “. With the curly ones the code doesn’t work…
Thanks
// just another way to write the same thing.
$(window).load(function() {
// everything’s finished loaded code here…
});
how can i add this to blogger template??
I’m trying to load page before load the map ( I use is map extreme ) although I use document ready function, the page trying to load mapLabels with page objects. for a while user see my page as there is no css. After the map finished it’s job the css loaded. How can I fix this? I use your code but it didn’t work for me.
Here is an example;
j(window).bind(“load”, function () {
MapXtremeJavascriptLib.init();
});
/* DOCUMENT READY FUNCTION AREA START */
j(document).ready(function () {
j(“.accordion”).accordion({
autoHeight: true,
navigation: true,
collapsible: true
});
/* DOCUMENT READY FUNCTION AREA END*/
Genius! Thank you so much :-)
Hi all,
I’m battering my head about how I can A: Tell a javascript to wait till the entire site has loaded. I guess I just found the solution to that. And B: Have the javascript load only on the home page.
The site runs on joomla and before I had the script run from a custom html module which I had assigned to the debug position and set to only load on the home page.
That worked fine for firefox, chrome and safari but IE was mocking up (as usual).
Now I’m battering my head about how I can add it so that the script (a lady walking into the site and welcoming the visitors) will only show on the home page.
I appreciate any suggestions!
Thanks in advance,
Andy
thanks, chris before knowing this I used setTimeout, and the bad thing using this, is we never exactly know how long the page load time on the user browser
Shame it doesn’t work in IE9 :-(
$(window).load(function()Should work fine on IE9. At least it is for me. Doesn’t work on IE8 and below though.
What it doesn’t work on however if the current version of Firefox. Apparently firefox treats .load and .ready as the same and loads .load after the javascript has loaded and doesn’t wait for all images and other page content like the other browsers do. Does anyone know of a workaround for firefox?
Thanks
~ Ben
Your averaging 1 answer a day for me. Thank you very much!
working…..
This is exactly what I needed, Thanks for the great resource!
Holy crap. This just saved my life.
… yes, I might be exaggerating, but not by much.
After spending about four hours trying to get some externally sourced code to cycle with jQuery cycle, this snippet solved everything.
Thanks!
Great… jQuery again. Looking at the title in Google, I thought you were going to present something non-framework. Bah, why do people ignore the fact that it’s not cool to load a 32KB jQuery lib for something that could be done in a few lines.
Thanks anyway… even when this didn’t help me at all.
You and your users probably have jQuery cached on their systems so I guess not an issue – plus if you use Google as the CDN its blindingly fast – so why not?
This is the most awesome website about web development!
I want to Load my Minified JS after the page load.
Currently it takes 1.8 sec to 2.5 sec to load this JS File which is increasing my complete webpage load to 4.2 sec to 5.1 sec.
So how can i load this JS without delaying the page load.
As always, jump on google have a nosey and the first result from Css-Tricks is the winner on the day!, Cheers for your time invested in the web dev commnity Chris.
$(window).on(‘load’, function(){
// AWESOMENESS
})
This snippet made my day Chris thank you a many man, I’m finding more and more useful things on your website for my front-end development, thanks !!
Thank you! Been struggling to get Jquery to load after a ton of images have been loaded and this just works perfectly!
As Jubal points out .bind has been deprecated in flavor of .on
$(window).bind(“load”, function(){
});
The above function works fine only on the initial load. But when we navigate to differect page and come back to the same page. the function gets executed before complete loading of Dom elements. Kindly tell me if there is another way