5

I have a header on my webpage that contains the some javascript for some jquery plugins i am using. These plugins are used on a bunch of pages so I just include them in the header document that is included in everyone one of my pages.

There is one page however that I would like to include some other javascript but is only needed on this one page. can i use the document ready function a second time on the same page or is that poor form?

I don't want to include the javascript on everypage as it is not needed and would be a waste to load on every page.

Sackling
  • 1,780
  • 5
  • 37
  • 71

4 Answers4

8

YES

The jQuery docs are clear that this is fine.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
3

no nothing wrong it will be executed in the order it is specified

here is a demo http://jsfiddle.net/LKuz2/7/

Rafay
  • 30,950
  • 5
  • 68
  • 101
  • 1
    Not sure if you're aware of this; but `e` is not an event object but rather the jQuery function. (Your naming may be a little ambiguous.) – pimvdb Jan 19 '12 at 20:58
  • @pimvdb you are right i was not aware of it tnx for correcting, updated the fiddle – Rafay Jan 19 '12 at 21:05
3

Yes, you can use it multiple times. It binds each function to the triggered event of the document being ready.

Will Stern
  • 17,181
  • 5
  • 36
  • 22
2

Yes, keeping in mind that:

  • Your code will be less readable.
  • You will end up with multiple function scopes with each block. So variables you create in one closure will not be visible from other ones.

Recommendation: If you think you need multiple $(document).ready( blocks, you are probably wrong. Take time to re-work your solution to something more maintainable.

Erm, and this has been covered here before. More than once. By Me. How embarrassing :)

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406