views:

88

answers:

2

Hi All, I was working with $(window).load() ...and found one issue. Actually as per this function first window should load and then any implementation under this function should start working. This works fine for IE but not for FF. in FF its working like $(document).ready()

can somebody suggest any alternate approach, or the reason why FF behave like this.

+6  A: 

Your second method is the preferred method, working for both FF and IE:

$(document).ready(function(){ /*.code.*/ });

Or the short-hand form:

$(function(){ /*.code.*/ });
Jonathan Sampson
+1 because I didn't know $(function(){ .... });
Derek Adair
thats fine, but sometimes u do need $(window).load()
Wondering
@Wondering - I'm curious, what is the use case of load() over ready()? (not saying there isn't, I really am curious)
Matt
@Wondering: When do you need to use `$.load()` on the window?
Jonathan Sampson
lets imagine a scenario.u have one span, and that sapn is getting populated through one javascript. lets say after populating its value is <span id="myspan">Hello</span> , now in another javascript file if u want to read this value , u will do $("#myspan").text()... now on ready(), this will return undefine,coz untill and unless ur window is loaded u will not be able to retrive value of #myspan....in this case in IE if u use Window,load() it will retrive value, coz it waits till window loads itself, same thing doesnt work for FF
Wondering
not sure if I have made myself clear :-)
Wondering
@Wondering: You don't use `$(window).load()` for that. You use the second method mentioned above: `$(function(){ /*...*/ });` and there would be absolutely no problems.
Jonathan Sampson
@Jonathan I think the subtle difference is that he has another JavaScript script setting the value of the span. Which I presume means this javascript itself doesn't run until ... apparently the page is "loaded". So, using ready() this span won't have it's value yet. @Wondering - when does this first script get run? Is it not also using the ready() event handler?
Matt
@Matt u r rite..no, its a different javascript file altogether...And its javascript, not jquery(I mean no jquery synatax)...sorry for my late response..I was away :-(
Wondering
+4  A: 

To add to Jonathans answer, jQuery's own documentation regarding $(document).ready:

"While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts."

source: http://api.jquery.com/ready/

EDIT:

Per your comments, it sounds like you are dependent on another javascript executing before you want your script to execute to grab the value. I can think of two options here:

1) If the other script is in your control, then have it invoke the code you want in your load() functions.

2) If this other code is not in your control, then your best best is to "observe" the field of interest for when it changes, so you know when to invoke your own script. I don't believe jQuery has this functionality built in (for non form fields), but there are plugins (such as http://plugins.jquery.com/taxonomy/term/1939).

Matt
thnx Matt for all ur help..will try..
Wondering