Wikipedia:User scripts/Guide: Difference between revisions

Content deleted Content added
Beginning update of documentation
Userscript structure: Change to use jQuery.ready()
Line 16:
== Userscript structure ==
 
The personal <tt>/vector.js</tt> and the [[Wikipedia:Gadget|gadgets]] are included early in the generated [[HTML]] page. This means that in most cases we need to defer the script actions until the page is loaded. The most common way is to use a special function, [http://api.jquery.com/ready/ <code>addOnloadHook.ready()</code>] from wikibits.js[[jQuery]].
 
<source lang="javascript">
Line 24:
};
 
// Schedule it to run after the HTML page is rendered
addOnloadHook$( document ).ready( myScript );
 
// This shorthand is also valid
jQuery( myScript );
</source>
 
Since the function is called only once, many users prefer to shorten this code with an "anonymous function call":
<source lang="javascript">
addOnloadHook$( document ).ready( function() {
//... code ...
} );
 
// Or
jQuery( function() {
//... code ...
} );
</source>
 
'' '''Note:''' <code>$</code> and <code>jQuery</code> are both the same object, the use between the two is simply your preference ''
 
Many scripts use this function simply to add some script interface, such as a link in a leftside portlet. Then the main part of the code is executed after the user clicks on that link.