MediaWiki talk:Common.js

How to Load JavaScript Files
There are various ways to load JavaScript pages in a MediaWiki environment. This section will attempt to compare and contrast different ways of loading JavaScript files.

All of these methods of loading a JavaScript file issue something called an HTTP request, which is basically where the client (your computer) asks the server (Wikia) to fetch a file. The fewer communications between client and server, the faster the webpage loads.

importScript

 * (for JavaScript newbies)

The global JavaScript function  is part of the MediaWiki software and can be used to asynchronously load a script stored on the same wiki (same domain).

importScriptPage

 * (for JavaScript newbies)

The global JavaScript function  is Wikia-specific, it cannot be used on non-Wikia wikis. It is designed to asynchronously load a script stored on any Wikia wiki (any .wikia.com domain)

importScriptURI

 * (more difficult, more customizable)

The global JavaScript function  is part of the MediaWiki software. It can be used to asynchronously load any JavaScript file stored anywhere on the internet.

URLs for wiki pages may need to have certain parameters tagged on when using. Here are a few basic examples. For more details, see mw:Manual:Parameters_to_index.php.
 * action=raw
 * tells the MediaWiki software to interpret the file as raw text


 * templates=expand
 * tells the MediaWiki software to expand any templates in the file


 * maxage=0
 * bypasses your computer's cache and gives you an up-to-date copy of the file


 * smaxage=0
 * bypasses the server's cache and commands the server to issue an up-to-date copy of the file

MediaWiki:LocalImports.js: http://terraria.wikia.com/wiki/MediaWiki:Common.js/LocalImports.js?action=raw&ctype=text/javascript&maxage=0&smaxage=0&templates=expand

mw.loader.load

 * (more difficult, more customizable)

The global JavaScript function  can be used the same way as. It was introduced in MediaWiki 1.17 as part of the Resource Loader, documentation available at ResourceLoader/Default_modules.

$.getScript

 * (very advanced, recommend not to use this if you don't know JavaScript/jQuery)

The jQuery method  can also be used to import a JavaScript file, but it has some major differences from all the preceding methods:
 * 1) It's a jQuery method, it works on any website that has jQuery, not just in a MediaWiki environment. Of course, Wikia uses the jQuery library, so  is available here.
 * 2) It can attach an onload event handler, so you don't have to load scripts asynchronously. You can ask for the script to be loaded, and then you can say "when the script has finished loading -- and no sooner -- do something else that depends on that JS file being completely loaded". By contrast, all of the above methods of importing a script file are asynchronous. So if you have multiple JavaScript files that depend on one another, well, be aware that you don't know *when* they'll be done loading if they're loading asynchronously, so things might go horribly wrong.

Besides these 2 points though, you can use  in pretty much the same way you use.

MediaWiki:Common.js on the Terraria Wiki
How it works:
 * 1) MediaWiki:Common.js has 2 distinct pieces
 * 2) Loads MediaWiki:Common.js/LocalImports.js which contains all local JavaScript
 * 3) Loads any JavaScript files from other, non-terraria.wikia.com domains (right now there are none)
 * 4) MediaWiki:Common.js/LocalImports.js transcludes various JS files as templates. Transcluding templates happens server-side = much faster than importing a JavaScript file with an HTTP request.
 * 5) * is faster than importScriptPage(script file)

More Info

 * For a list of all JavaScript files on the Terraria Wiki, simply browse Special:PrefixIndex/MediaWiki:Common.js, as all JavaScript pages are now subpages of Common.js. Note that currently, not all of the JavaScript files are actually used. Some of them are outdated / may be used again in the future / just kept for historical reasons. Refer to LocalImports.js for the list of script files that are actively being used.
 * MediaWiki:Wikia.js has been blanked and everything has been migrated to LocalImports.js. Of course, scripts that should only load in the Wikia skin (not in Monobook) will still do so.
 * If you have a ton of import statements in your global.js, you can potentially take advantage of this trick to speed up JavaScript loading times. For example, see w:c:dev:User:Pecoes/global.js