MediaWiki Developer's Handbook/Add JavaScript/Predefined variables

URL constructors and server information
JS var wgScript = "/w/index.php"; var wgArticlePath = "/wiki/$1": var wgServer = "//en.wikibooks.org"; var wgCookiePrefix = "enwikibooks"; Using  and , you can build a valid URL for the wiki. This is most useful for diffs and other action links.

To go to a page on Wikimedia wikis, you know that http://lang.family.org/wiki/article will take you where you want to go, but not all wikis have short URLs - in that case, use  to know how you construct an article path, or just use   and.

Variable  can be used by replacing $1 by the page title:.

Page information
JS var wgCanonicalNamespace = ""; var wgCanonicalSpecialPageName = false; var wgNamespaceNumber = 0; var wgPageName = "MediaWiki_Developer\'s_Handbook/Add_JavaScript"; var wgTitle = "MediaWiki Developer\'s Handbook/Add JavaScript"; var wgAction = "view"; var wgIsArticle = true; var wgCanonicalNamespace = "Special"; var wgCanonicalSpecialPageName = "Specialpages"; var wgNamespaceNumber = -1; var wgPageName = "Special:SpecialPages"; var wgTitle = "SpecialPages"; The canonical namespace never changes based on localization. For example, on kmwiki, their meta or project namespace has, and on km:ជំនួយ:Editing,.

As well, each namespace has a number, beginning with zero for the main namespace. These numbers are customizable.

This is helpful to make scripts portable - you don't have to worry about knowing the localized name for a namespace - just use the canonical name, or the number.

Since this page is in the main namespace,, but on About, that is not the case: JS var wgPageName = "Help:About"; var wgTitle = "About"; wgTitle never includes the namespace prefix, while wgPageName does. Note also that wgPageName replaces spaces with underscores.

wgAction is one of: This exposes what the user is doing on the current page. Be careful: to the user, view, submit and purge are probably not differentiated in many cases. In some cases, you will want your script to run on all of those.
 * view
 * submit (after clicking Save page, Show preview, or Show changes)
 * history
 * edit
 * delete
 * protect
 * purge (when doing  to purge the server cache, forcing the page to be re-rendered)

wgAction is often useful to avoid running your whole script when it isn't needed. If your script changes the page history form, then you can do JS if ( wgAction == 'history' ) {	addOnloadHook( changeHistoryForm ); } function changeHistoryForm {	... }

wgIsArticle tells you whether the page is an article as opposed to a special page.

Special Pages
JS var wgCanonicalNamespace = "Special"; var wgCanonicalSpecialPageName = "Specialpages"; var wgNamespaceNumber = -1; var wgPageName = "Special:SpecialPages"; var wgTitle = "SpecialPages"; For special pages, the canonical namespace is "Special", and wgCanonicalSpecialPageName is the non-CamelCase name of the special page in English. Again, the canonical form doesn't change with localization, so it's better to use that. As well, all special pages have the namespace number -1.

User information
JS var wgUserName = "Mike.lifeguard"; var wgUserGroups = ["bureaucrat", "checkuser", "editor", "sysop", "*", "user", "autoconfirmed"]; var wgGlobalGroups = ["Global_rollback", "steward"]; The user's username is set as wgUserName, and their local and global groups are in arrays. Note that the global group global rollback has an underscore, not a space.

This is useful when part of a script requires that the user be in a certain user group, typically sysop: JS function userIsInGroup( group ) {	// are they in a given local group?	if ( wgUserGroups ) {		if ( !group