0

Is there a way to force widgets to output jquery init() functions in one '$(document).ready()'?
It should look like this:

$(document).ready(function($) {
     $('.style1').plugin1Init();
     $('.style2').plugin2Init();
     $('.style3').plugin3Init();
});

I'm asking because I'm trying to speed up my wordpress theme. I have several widgets which outputs jquery code in $(document).ready() functions.

Three '$(document).ready()' functions reduces page loading speed almost three times according to Jquery speed test

Example code on a page when I'm using 3 widgets:

$(document).ready(function($) {
     $('.style1').plugin1Init();
});

$(document).ready(function($) {
     $('.style2').plugin2Init();
});

$(document).ready(function($) {
     $('.style3').plugin3Init();
});
2
  • 6
    Having multiple document ready blocks is not a problem at all. There is only a single event which will call all registered functions in a loop. If your page loads slowly it's due to something else. Commented Jun 15, 2012 at 17:46
  • if you link to a test, make sure you check the latest one Commented Jun 16, 2012 at 0:57

1 Answer 1

1

You can call $(document).ready(function($) { once

See:

$(document).ready(function($) {
    $('.style1').plugin1Init();
    $('.style2').plugin2Init();
    $('.style3').plugin3Init();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.