views:

1982

answers:

4

I am looking at an asp.net 2 web application that I am maintaining (but did not write).

Some things that should happen on the page loading do not, but only sometimes, and it seems to be if you are using Firefox 3 inside a VM. JQuery and asp.net Ajax are used.

The important function that should run every time (but does not) is attached by the following Javascript:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){ Sys.Application.add_load(ImportantFunction); });   
$(document).ready(function(){ Otherstuff(); });
$(document).ready(function(){ MoreStuff(); });
//]]>
</script>

But if I use firebug to set a breakpoint inside ImportantFunction(), it is not hit on page load in firefox 3, but it is hit on an ajax update.

In the page there are multiple calls to $(document).ready since they come from different parts of the asp.net code behind it. Yes, they do all execute.

+1  A: 

According to this recent blog posting, this is a bug (or at least a misfeature) in FireFox 3. He suggests naming your important function PageLoad to get it to work cross-browser, though I'm not sure if that will work or not.

Adam Bellaire
That function is in a library and is called elsewhere, so I'm not going to rename it. But I could make a wrapper.
Anthony
+2  A: 

You're using jQuery to attach a "load" method which then in-turn attaches a load event and I think this is where your problem is.

$(document).ready and Sys.Application.add_load are pretty much the same, or so my understanding goes. I'm not really sure in which order the browser will execute them though.

I'd suggest removing the Sys.Application.add_load wrapper call on ImportantFunction so you're not trying to attach to an event stack that has already fired.

Slace
+1  A: 

Try this:

<script type="text/javascript">
//<![CDATA[
$(document).ready(ImportantFunction);   
$(document).ready(Otherstuff);
$(document).ready(MoreStuff);
//]]>
</script>

Put the call to Sys.Application.add_load in the body of ImportantFunction, i.e in your .js file:

function importantFunction()
{
   Sys.Application.add_load(ImportantFunction);
}

Edit: I'm not sure if its possible to add multiple functions to be run on $(document).ready event. It might help if you did this instead:

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(init);   
    //]]>
    </script>

And in init you can include calls to all the other functions, i.e:

function init()
{
   importantFunction();
   otherStuff();
   moreStuff();
   //Any other functions to be called upon page load go here
}

It will also make the code easier to read :)

Click Upvote
Give my example a shot anyway, it works for me so it could work for you too.
Click Upvote
The posts that I have looked up indicate that you can attach more than one block of code to $(document).ready. http://www.learningjquery.com/2006/09/multiple-document-ready
Anthony
Winner!. The inital hack fix looks like this:$(document).ready(function(){ ImportantFunctionOnLoad(); }); function ImportantFunctionOnLoad() { ImportantFunction(); Sys.Application.add_load(ImportantFunction); } need to clean up and make sure that ImportantFunction isn't called twice.
Anthony
multiple $(document).ready() calls are allowed, they will all be executed.
Pim Jager
+2  A: 

Is there any reason why you can't use the ASP.NET AJAX pageLoad function instead of $(document).ready()?

function pageLoad(sender, args)
{
     ImportantFunction();
     OtherStuff();
     MoreStuff();
}

This is part of the ASP.NET AJAX client page lifecycle and all JavaScript code inside will be executed on every page load, including asynchronous postbacks.

Russ Cam
That also works.
Anthony
Here's an upvote for the good answer :)
Click Upvote