views:

578

answers:

3

I'm trying to use jQuery UI tabs much like a Master Page in ASP.Net. I have specific javascript that needs to run once each tab is loaded. I've tried to have a $(document).ready function in each page, but I've read that's bad practice and only seems to work in IE.

I know there's a load event, but I need to the event to be specific for each tab and I'm not sure how to accomplish this, or if it's even possible. I may have to revert to using and actual MasterPage with postbacks for each page load.

+1  A: 

Where did you read that it's bad practice?

Afaik, having multiple $(document).ready()'s will simply make them "merge", so that all code that is in either of them will run. I don't remember if that's new in jQuery 1.3 though, so you might want to check that out.

Tomas Lycken
+1  A: 

It is not bad practice to have multiple $().ready's, unless you've needlessly broken it up into multiple ones. In the case of a tab being loaded in ajaxily, IMHO putting a $(document).ready inside the loaded-in content is probably the best way to go about having some tab-specific actions happen. I use that for re-binding the newly loaded elements as they are injected into the freshly loaded tab.

karim79
If you are binding .click events and such, take a look at the (new) .live() events in jQuery 1.3. They'll do the work for ya!
Tomas Lycken
+2  A: 

$(document).ready() is not bad practice, and it works in every browser that jQuery works in. If your events are only firing in IE, you've got a bug somewhere in your own code.

I have dozens of ready functions in my applications and no problems.

Plutor
Thanks to everyone. I did some more research. I was including a reference to jQuery in my subpage for testing and, for some reason, this caused a problem with the rest of the javascript I written in the page."In general you shouldn't treat the loaded element as a new page and call the $(document).ready. This is not a new page, but some new elements added to the DOM. All ajax methods feature a callback method that is invoked, when the data are successfully loaded."That's what I read telling me it might be the wrong way.I have it working now. Thanks again!
Kyle