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

Darbio
  • 11,286
  • 12
  • 60
  • 100

3 Answers3

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.

fish2000
  • 4,289
  • 2
  • 37
  • 76
  • 1
    Cufon was the culprit... Removed! – Darbio Nov 02 '10 at 00:41
  • 4
    The $(window).load() tip helped me! – YeahStu Jan 19 '11 at 19:50
  • 1
    If $(window).load() works but $(document).ready() doesn't, what does that mean? – CharlieMezak May 16 '11 at 19:18
  • @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. – fish2000 Sep 01 '11 at 17:29
  • 2
    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. – Andy Sep 02 '12 at 04:25
1

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

Ben
  • 20,737
  • 12
  • 71
  • 115
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.

Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80