Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Difference between AJS.toInit and AJS.$(document).ready

Yagnesh Bhat
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 24, 2014

What is the exact difference between AJS.toInit and AJS.$(document).ready()? Are there any places where one should be preferred over the other?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
ivan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 28, 2014

This the source for AJS.toInit:

// Override of atlassian.js toInit to add improved exception logging
AJS.toInit = function (func) {
    $(function () {
        try {
            func.apply(this, arguments);
        } catch(ex) {
            // just calling AJS.log with an Error object gives "[object Error]" in IE,
            // We want better, thus more plumbing for better Error object logging
            AJS.logError("Failed to run init function: ", ex);
            AJS.log("Failed toInit function is: " + func);
        }
    });
    return this;
};

where we can see it is just a wrapper (with some additional error handling) in top of jQuery's DOM ready shorthand

$(function() {
    console.log( "ready!" );
});

which becomes

$(document).ready(function() {
    console.log( "ready!" );
});

in its extended notation (they are equivalent)

http://learn.jquery.com/using-jquery-core/document-ready/

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 29, 2014

Thanks for detailed answer!

Yagnesh Bhat
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 29, 2014

Got it. So in a nutshell, AJS.toInit is a superset of $(document).ready(). So I guess like all other AJS functions (AJS.log, etc), we need to prefer AJS.toInit over $(document).ready.

0 votes
Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 25, 2014

Something I have found on Atlassian's website,

Please be reminded to use the toInit method everytime AJS is called. 
This will ensure that the JQuery codes are called only after AJS has been succesfully initialized. 
In order to use the toInit method, include the following:
 
AJS.toInit(function(){
    <!-- INSERT THE JS CODES HERE -->
});

https://confluence.atlassian.com/display/CONFKB/How+to+Use+JavaScript+in+Confluence

but, as a plugin developer, I am also interested to know more details...

Yagnesh Bhat
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 26, 2014

Yup, I too saw the same in their site. But most of the time I have seen developers using both interchangeably. I use AJS.toInit() most of the time and it seems to do the job well. However, I am curious to know when and how does AJS.$(document).ready() comes into picture or probably AJS.toInit() is something like AJS.$(document).ready()'s big brother :) Atlassians, could you please throw more light on this?

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 28, 2014

Other syntax: AJS.$(function(){...})

TAGS
AUG Leaders

Atlassian Community Events