jQuery $(document).ready() not firing
Asked Answered
C

3

12

Using jQuery 1.4.2 from Google hosted Code.

Is there a reason why the following javascript does not fire all 3 document.ready functions when the document is ready?

The first $(document).ready() function, which renders headers, and the second, which gives a 'Foo' alert box triggered, but subsequent ones in new <script> blocks aren't triggered,

<script type="text/javascript">
    $(document).ready(function () {
        Cufon.replace('h1'); // Works without a selector engine
        Cufon.replace('h2'); // Works without a selector engine
        Cufon.replace('h3'); // Works without a selector engine
        Cufon.now();
    });
    $(document).ready(function () { alert("Number Foo"); });
</script>

// html tags

<script type="text/javascript">
    $(document).ready(function () { alert("Number One"); });
    $(document).ready(function () { alert("Number Two"); });
</script>

These are in seperate web parts, hosted on the same page in Sharepoint2010

Corpsman answered 20/10, 2010 at 4:1 Comment(6)
I'd be a bit wary of defining a document ready function inside another document ready function.Madisonmadlen
Sorry, was a typo. Post updated.Corpsman
The last $(document).ready function is not well terminated. }); missingDirector
Good spot, but that's not the problem...Corpsman
Host jquery full locally then put a debugger statement inside the ready iterator and see for yourself. I suspect you must have an error but without more source it is impossible to seeHalitosis
Each script tag is in a separate web part? We didn't know this before. The problem itself is not in the scripts. Something else in your page is messing up your codeDirector
S
23

I can think of three forensic things to try, right off:

  1. try it with non-google-hosted libraries.
  2. comment out the Cufon calls -- I believe Cufon does some crazy stuff to download additional resources, yes? That may be interfering.
  3. sub in $(window).load() for one or more of your $(document).ready() callback defs. They have different firing criteria -- $(window).load() waits for everything to load up, allegedly -- but the substitution may be revealing.

Of course, console.log() and alert() will be your in-leu-of-debugger-breakpoint best friends in this case.

Shotten answered 20/10, 2010 at 7:54 Comment(5)
Cufon was the culprit... Removed!Corpsman
The $(window).load() tip helped me!Arriviste
If $(window).load() works but $(document).ready() doesn't, what does that mean?Unforgettable
@CharlieMezak: does your script depend on an asset that it assumes is loaded (like an image)? I believe $(document).ready() callbacks fire once all of the documents' scripts, CSS, and HTML are loaded, without waiting for images and other referents.Shotten
Switching to $(window).load() fixed the problem for me but I have no idea why. If anyone could shed any light that would be helpful.Assertive
P
1

you're missing a closing curly bracket and parenthesis in the second script tag

Palace answered 20/10, 2010 at 4:14 Comment(3)
Nope, this isn't the problem.Corpsman
Did you typed your code incorrectly from the beggining or corrected it after the answers?Director
I changed it just before your first answer in the comments of the question.Corpsman
S
1

You are missing a }); in the end of the last $(document).ready

Once you correct this it should work

EDIT: Since you say now that each script tag is in a separate web part I believe the problem itself is not in the scripts. Something else in your page is messing up your code.

Stretchy answered 20/10, 2010 at 4:15 Comment(1)
This isn't the problem, as I have fixed the source and it is still not working.Corpsman

© 2022 - 2024 — McMap. All rights reserved.