What is the exact difference between AJS.toInit and AJS.$(document).ready()? Are there any places where one should be preferred over the other?
Community moderators have prevented the ability to post new answers.
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)
Thanks for detailed answer!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Other syntax: AJS.$(function(){...})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.