Wikipedia:User scripts/Guide: Difference between revisions

Content deleted Content added
Built-in scripts: Clarified some wording.
Userscript structure: Some wording fix and some code prettifying.
Line 16:
== Userscript structure ==
 
PersonalThe personal <tt>/monobook.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 <code>function addOnloadHook()</code> from wikibits.js.
 
<source lang="javascript">
Line 22:
function myScript(){
//... code ...
};
 
//Schedule it to run after the HTML page is rendered
addOnloadHook(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( function() {
//... code ...
} );
</source>
 
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.
 
== Editing and loading the user script ==