Work in progress — This wiki is an active migration / mirror of Homestar Runner Wiki and is not finished yet. Account registration and public editing are turned off until the migration is further along. For status updates, see the migration thread on the forums.

Help:JavaScript: Difference between revisions

From HR Wiki Twice
Jump to navigationJump to search
imported>HRWikiMirrorBot
Reimported from hrwiki.org Wayback HTML→wikitext (full reconvert)
 
imported>HRWikiMirrorBot
Reimported from hrwiki.org Wayback HTML→wikitext (full reconvert)
 
Line 1: Line 1:
<!-- Mirrored from Homestar Runner Wiki (hrwiki.org) via Wayback Machine -->
<!-- Wayback HTML body (hrwiki.org); RawHtml -->
You can add JavaScript to any page, to let you customize how the page looks. If you use the Monobook skin (the default) you can put the Javascript [/wiki/Special:MyPage/monobook.js here]. If you use Tavistyle you can put it [/wiki/Special:MyPage/tavistyle.js here], or if you use Cologne Blue you can put it [/wiki/Special:MyPage/cologneblue.js here].
<html>
 
<p>You can add JavaScript to any page, to let you customize how the page looks. If you use the Monobook skin (the default) you can put the Javascript <a href="/index.php?title=Special:MyPage/monobook.js" title="Special:MyPage/monobook.js">here</a>. If you use Tavistyle you can put it <a href="/index.php?title=Special:MyPage/tavistyle.js" title="Special:MyPage/tavistyle.js">here</a>, or if you use Cologne Blue you can put it <a href="/index.php?title=Special:MyPage/cologneblue.js" title="Special:MyPage/cologneblue.js">here</a>.
There are a lot of "helper" functions available. To use them, put {{subst:[[Template:utilities|utilities]]}} at the top of this page. When you save, this will become
</p><p>There are a lot of "helper" functions available. To use them, put <code>{{subst:<a href="/index.php?title=Template:utilities" title="Template:utilities">utilities</a>}}</code> at the top of this page. When you save, this will become
 
</p>
<pre>// include utility functions
<pre>// include utility functions
document.write('<script type="text/javascript" src="/index.php?title=User:Phlip/utilities.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');
document.write('&lt;script type="text/javascript" src="/index.php?title=User:Phlip/utilities.js&amp;action=raw&amp;ctype=text/javascript&amp;dontcountme=s"&gt;&lt;/script&gt;');
</pre>
</pre>
<p>which will include all the relevant code. If you want to stop using them, just remove those two lines.
</p><p>They have been tested in Firefox and Internet Explorer (5.0, 5.5 and 6.0).
</p><p>If you don't understand JavaScript, feel free to skip down to <a href="#Examples" title="">Examples</a> and mix &amp; match the pieces into the template in <a href="#General" title="">General</a>.
</p>


which will include all the relevant code. If you want to stop using them, just remove those two lines.
<a id="General" name="General"></a><h2> <span class="mw-headline">General</span></h2>
 
<p>You need to set up the code to be run when the page has loaded (otherwise it will try to edit a page that doesn't exist yet). Your JavaScript page should basically look like:
They have been tested in Firefox and Internet Explorer (5.0, 5.5 and 6.0).
</p>
 
If you don't understand JavaScript, feel free to skip down to [#Examples Examples] and mix & match the pieces into the template in [#General General].
 
== General ==
 
You need to set up the code to be run when the page has loaded (otherwise it will try to edit a page that doesn't exist yet). Your JavaScript page should basically look like:
 
<pre>{{subst:utilities}}
<pre>{{subst:utilities}}


function doeverything()
function doeverything()
{
{
   // code goes here
   <span style="font-family: sans-serif"><i>// code goes here</i></span>
}
}
if (window.addEventListener) window.addEventListener("load",doeverything,false);
if (window.addEventListener) window.addEventListener("load",doeverything,false);
else if (window.attachEvent) window.attachEvent("onload",doeverything);
else if (window.attachEvent) window.attachEvent("onload",doeverything);
</pre>
</pre>
 
<p>and put the things below where it says <i>// code goes here</i>.
and put the things below where it says ''// code goes here''.
</p>
 
<a id="Available_functions" name="Available_functions"></a><h2> <span class="mw-headline">Available functions</span></h2>
== Available functions ==
<p>All these functions are on an object called <code>utilities</code>. So to call them you need to type <code>utilities.</code><i>function</i><code>(</code><i>parameters</i><code>)</code>.
 
</p>
All these functions are on an object called utilities. So to call them you need to type utilities.''function''(''parameters'').
<a id="Information" name="Information"></a><h3> <span class="mw-headline">Information</span></h3>
 
<a id="getArticleFullTitle" name="getArticleFullTitle"></a><h4> <span class="mw-headline">getArticleFullTitle</span></h4>
=== Information ===
<p><code>utilities.getArticleFullTitle()</code>
 
</p><p>Returns the title of the current page, like "Help:JavaScript".
==== getArticleFullTitle ====
</p>
 
utilities.getArticleFullTitle()
 
Returns the title of the current page, like "Help:JavaScript".
 
<pre>alert("You're looking at " + utilities.getArticleFullTitle());
<pre>alert("You're looking at " + utilities.getArticleFullTitle());
</pre>
</pre>
 
<a id="getArticleTitle" name="getArticleTitle"></a><h4> <span class="mw-headline">getArticleTitle</span></h4>
==== getArticleTitle ====
<p><code>utilities.getArticleTitle()</code>
 
</p><p>Returns the title of the current page without the namespace, like "JavaScript". This is useful if you want to be able to find the associated talk page (see <a href="#talknamespace" title="">talknamespace</a>)
utilities.getArticleTitle()
</p>
 
<a id="getArticleNamespace" name="getArticleNamespace"></a><h4> <span class="mw-headline">getArticleNamespace</span></h4>
Returns the title of the current page without the namespace, like "JavaScript". This is useful if you want to be able to find the associated talk page (see [#talknamespace talknamespace])
<p><code>utilities.getArticleNamespace()</code>
 
</p><p>Returns the <a href="/index.php?title=Help:Namespaces" title="Help:Namespaces">namespace</a> of the current page. Note that this returns a <a class="extiw" href="/index.php?title=Help:Namespace" title="metawikipedia:Help:Namespace">magical number</a> – to get the actual name, see <a href="#namespaces" title="">namespaces</a> below.
==== getArticleNamespace ====
</p>
 
<a id="getArticleURL" name="getArticleURL"></a><h4> <span class="mw-headline">getArticleURL</span></h4>
utilities.getArticleNamespace()
<p><code>utilities.getArticleURL()</code>
 
</p><p>Returns the URL for the page you're looking at. Note that this is a link to the <i>article</i>, not the exact page you're looking at – for example if you're editing a page, it will still link to the real page (not the edit page). If you want the full link, use <code>window.location</code>.
Returns the [[Help:Namespaces|namespace]] of the current page. Note that this returns a [[Help:Namespace|magical number]] – to get the actual name, see [#namespaces namespaces] below.
</p>
 
<a id="localurl" name="localurl"></a><h4> <span class="mw-headline">localurl</span></h4>
==== getArticleURL ====
<p><code>utilities.localurl(</code><i>page</i> [<code>, </code><i>namespace</i> [<code>, </code><i>extras</i>]]<code>)</code>
 
</p><p>Returns the URL of the given page. Again, the namespace should be a number. Examples:
utilities.getArticleURL()
</p>
 
Returns the URL for the page you're looking at. Note that this is a link to the ''article'', not the exact page you're looking at – for example if you're editing a page, it will still link to the real page (not the edit page). If you want the full link, use window.location.
 
==== localurl ====
 
utilities.localurl(''page'' [, ''namespace'' [, ''extras'']])
 
Returns the URL of the given page. Again, the namespace should be a number. Examples:
 
<pre>utilities.localurl("Main Page")
<pre>utilities.localurl("Main Page")
utilities.localurl("User:Me") // this works
utilities.localurl("User:Me") // this works
Line 75: Line 58:
utilities.localurl(utilities.getArticleTitle(), utilities.getArticleNamespace(), "action=edit") // an "edit" link for the current page
utilities.localurl(utilities.getArticleTitle(), utilities.getArticleNamespace(), "action=edit") // an "edit" link for the current page
</pre>
</pre>
 
<a id="namespaces" name="namespaces"></a><h4> <span class="mw-headline">namespaces</span></h4>
==== namespaces ====
<p><code>utilities.namespaces[</code><i>number</i><code>]</code>
 
</p><p>An array of the names of each namespace. This is blank for the Main namespace, and the name for all the others.
utilities.namespaces[''number'']
</p>
 
An array of the names of each namespace. This is blank for the Main namespace, and the name for all the others.
 
<pre>var ns = utilities.getArticleNamespace();
<pre>var ns = utilities.getArticleNamespace();
if (ns == 0)
if (ns == 0)
Line 88: Line 68:
   alert("You're looking at " + utilities.namespaces[ns] + "!");
   alert("You're looking at " + utilities.namespaces[ns] + "!");
</pre>
</pre>
 
<a id="namespacedescription" name="namespacedescription"></a><h4> <span class="mw-headline">namespacedescription</span></h4>
==== namespacedescription ====
<p><code>utilities.namespacedescription[</code><i>number</i><code>]</code>
 
</p><p>An array of the descriptions of each namespace – what the tabs are labeled with in Monobook. For example "Project Page" for a <a href="/index.php?title=HRWiki:The_Stick" title="HRWiki:The Stick">HRWiki namespace page</a>, etc.
utilities.namespacedescription[''number'']
</p>
 
<a id="articlenamespace" name="articlenamespace"></a><h4> <span class="mw-headline">articlenamespace</span></h4>
An array of the descriptions of each namespace – what the tabs are labeled with in Monobook. For example "Project Page" for a [[HRWiki:The Stick|HRWiki namespace page]], etc.
<p><code>utilities.articlenamespace[</code><i>number</i><code>]</code>
 
</p><p>An array of the article namespace for each talk namespace. For example 2 is "User" and 3 is "User talk", so <code>utilities.articlenamespace[2]</code> and  <code>utilities.articlenamespace[3]</code> are both 2.
==== articlenamespace ====
</p>
 
<a id="talknamespace" name="talknamespace"></a><h4> <span class="mw-headline">talknamespace</span></h4>
utilities.articlenamespace[''number'']
<p><code>utilities.talknamespace[</code><i>number</i><code>]</code>
 
</p><p>An array of the talk namespace for each article namespace. For example 2 is "User" and 3 is "User talk", so <code>utilities.talknamespace[2]</code> and  <code>utilities.talknamespace[3]</code> are both 3. <code>utilities.talknamespace[-1]</code> (the Special namespace) doesn't exist.
An array of the article namespace for each talk namespace. For example 2 is "User" and 3 is "User talk", so utilities.articlenamespace[2] and  utilities.articlenamespace[3] are both 2.
</p>
 
==== talknamespace ====
 
utilities.talknamespace[''number'']
 
An array of the talk namespace for each article namespace. For example 2 is "User" and 3 is "User talk", so utilities.talknamespace[2] and  utilities.talknamespace[3] are both 3. utilities.talknamespace[-1] (the Special namespace) doesn't exist.
 
<pre>var ns = getArticleNamespace();
<pre>var ns = getArticleNamespace();
var talkpage = utilities.localurl(utilities.getArticleTitle(), utilities.talknamespace[ns]);
var talkpage = utilities.localurl(utilities.getArticleTitle(), utilities.talknamespace[ns]);
</pre>
</pre>
 
<a id="Monobook-specific" name="Monobook-specific"></a><h3> <span class="mw-headline">Monobook-specific</span></h3>
=== Monobook-specific ===
<p>These functions only work in the Monobook skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
 
</p>
These functions only work in the Monobook skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
<a id="addMonobookQuickbarLink" name="addMonobookQuickbarLink"></a><h4> <span class="mw-headline">addMonobookQuickbarLink</span></h4>
 
<p><code>utilities.addMonobookQuickbarLink(</code><i>URL</i><code>, </code><i>title</i> [<code>, </code><i>where</i>]<code>)</code>
==== addMonobookQuickbarLink ====
</p><p>Adds a link to the "quickbar" (the bar with links to your user page, your talk page, logout, etc). "Where" should be one of:
 
</p>
utilities.addMonobookQuickbarLink(''URL'', ''title'' [, ''where''])
<ul><li><code>"userpage"</code> if you want the link to appear before your <b>userpage</b> link.
 
</li><li><code>"mytalk"</code> if you want the link to appear before your <b>my talk</b> link.
Adds a link to the "quickbar" (the bar with links to your user page, your talk page, logout, etc). "Where" should be one of:
</li><li><code>"preferences"</code> if you want the link to appear before your <b>preferences</b> link.
 
</li><li><code>"watchlist"</code> if you want the link to appear before your <b>my watchlist</b> link.
* "userpage" if you want the link to appear before your '''userpage''' link.
</li><li><code>"mycontris"</code> if you want the link to appear before your <b>my contributions</b> link.
* "mytalk" if you want the link to appear before your '''my talk''' link.
</li><li><code>"logout"</code> if you want the link to appear before your <b>log out</b> link.
* "preferences" if you want the link to appear before your '''preferences''' link.
</li><li>Leave it off entirely if you want the link to appear after your <b>log out</b> link.
* "watchlist" if you want the link to appear before your '''my watchlist''' link.
</li></ul>
* "mycontris" if you want the link to appear before your '''my contributions''' link.
* "logout" if you want the link to appear before your '''log out''' link.
* Leave it off entirely if you want the link to appear after your '''log out''' link.
 
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage")
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage")
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Outta Here")
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Outta Here")
</pre>
</pre>
 
<a id="addMonobookTab" name="addMonobookTab"></a><h4> <span class="mw-headline">addMonobookTab</span></h4>
==== addMonobookTab ====
<p><code>utilities.addMonobookTab(</code><i>URL</i><code>, </code><i>title</i><code>, </code><i>padding</i> [<code>, </code><i>at start</i>]<code>)</code>
 
</p><p>Adds a link to the tab bar (the bar with links to the page, the talk page, edit, history, etc). <i>padding</i> lets you group the tabs together (like edit and history are) – if it's set to <code>true</code> it will be separated from the other links, if you set it to <code>false</code> it won't be. <i>at start</i> says whether to add the tab to the start or to the end (the tab bar changes too much to be able to say "put it here"... some pages don't have an "edit" link if they're protected, Special pages only have the "special" tab...). If you leave <i>at start</i> off, it will put it on the end.
utilities.addMonobookTab(''URL'', ''title'', ''padding'' [, ''at start''])
</p>
 
<pre>utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "A", true)  //  \
Adds a link to the tab bar (the bar with links to the page, the talk page, edit, history, etc). ''padding'' lets you group the tabs together (like edit and history are) – if it's set to true it will be separated from the other links, if you set it to false it won't be. ''at start'' says whether to add the tab to the start or to the end (the tab bar changes too much to be able to say "put it here"... some pages don't have an "edit" link if they're protected, Special pages only have the "special" tab...). If you leave ''at start'' off, it will put it on the end.
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "B", false) //  } these links will all be grouped together
 
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "C", false) //  /
<pre>utilities.addMonobookQuickbarLink("http://www.somesite.com/", "A", true)  //  \
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Loner", true) //  this one will start a new group
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "B", false) //  } these links will all be grouped together
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "C", false) //  /
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Loner", true) //  this one will start a new group
</pre>
</pre>
 
<a id="addMonobookNavboxLink" name="addMonobookNavboxLink"></a><h4> <span class="mw-headline">addMonobookNavboxLink</span></h4>
==== addMonobookNavboxLink ====
<p><code>utilities.addMonobookNavboxLink(</code><i>URL</i><code>, </code><i>title</i> [<code>, </code><i>which one</i> [<code>, </code><i>at start</i>]]<code>)</code>
 
</p><p>Adds a link to the navigation boxes (the ones to the left). If <i>which one</i> is left off it will add to the "navigation" box. It can be set to:
utilities.addMonobookNavboxLink(''URL'', ''title'' [, ''which one'' [, ''at start'']])
</p>
 
<ul><li><code>"navigation"</code> to add to <b>navigation</b>
Adds a link to the navigation boxes (the ones to the left). If ''which one'' is left off it will add to the "navigation" box. It can be set to:
</li><li><code>"tb"</code> to add to the <b>toolbox</b>
 
</li><li><code>"ext"</code> to add to <b>external links</b>
* "navigation" to add to '''navigation'''
</li></ul>
* "tb" to add to the '''toolbox'''
<p>Again, <i>at start</i> can be <code>true</code> to add it to the top, or <code>false</code> (or leave it off) to add it to the bottom.
* "ext" to add to '''external links'''
</p>
 
Again, ''at start'' can be true to add it to the top, or false (or leave it off) to add it to the bottom.
 
<pre>utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons")
<pre>utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons")
utilities.addMonobookNavboxLink("http://www.somesite.com/", "Some Site", "ext")
utilities.addMonobookNavboxLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Some Site", "ext")
</pre>
</pre>
 
<a id="addMonobookBottomTabs" name="addMonobookBottomTabs"></a><h4> <span class="mw-headline">addMonobookBottomTabs</span></h4>
==== addMonobookBottomTabs ====
<p><code>utilities.addMonobookBottomTabs(</code>[<i>no CSS</i>]<code>)</code>
 
</p><p>Copies the tab bar to the bottom of the page. If you use <a href="#addMonobookTab" title="">addMonobookTab</a>, this should come <i>after</i> those (so the new tabs will be copied too). Normally this adds a lot of extra CSS to the page to get it to work – if you'd rather add the CSS yourself to <a href="/index.php?title=Special:MyPage/monobook.css" title="Special:MyPage/monobook.css">your custom CSS</a>, then you can pass a <code>true</code> for <i>no CSS</i> – usually you can just leave it off entirely and it will work without extra effort.
utilities.addMonobookBottomTabs([''no CSS''])
</p>
 
<a id="Cologne_Blue-specific" name="Cologne_Blue-specific"></a><h3> <span class="mw-headline">Cologne Blue-specific</span></h3>
Copies the tab bar to the bottom of the page. If you use [#addMonobookTab addMonobookTab], this should come ''after'' those (so the new tabs will be copied too). Normally this adds a lot of extra CSS to the page to get it to work – if you'd rather add the CSS yourself to [/wiki/Special:MyPage/monobook.css your custom CSS], then you can pass a true for ''no CSS'' – usually you can just leave it off entirely and it will work without extra effort.
<p>These functions only work in the Cologne Blue skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
 
</p>
=== Cologne Blue-specific ===
<a id="cologneBlueNewMessages" name="cologneBlueNewMessages"></a><h4> <span class="mw-headline">cologneBlueNewMessages</span></h4>
 
<p><code>utilities.cologneBlueNewMessages()</code>
These functions only work in the Cologne Blue skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
</p><p>Returns <code>true</code> if you nave new messages. The Cologne Blue "You have new messages" message isn't as "in your face" as the Monobook and TaviStyle ones are. This allows you to fix that, if you want.
 
</p>
==== cologneBlueNewMessages ====
 
utilities.cologneBlueNewMessages()
 
Returns true if you nave new messages. The Cologne Blue "You have new messages" message isn't as "in your face" as the Monobook and TaviStyle ones are. This allows you to fix that, if you want.
 
<pre>if (utilities.cologneBlueNewMessages())
<pre>if (utilities.cologneBlueNewMessages())
   alert ("Dude! Check your messages!");
   alert ("Dude! Check your messages!");
</pre>
</pre>
 
<a id="addCologneBlueQuickbarLink" name="addCologneBlueQuickbarLink"></a><h4> <span class="mw-headline">addCologneBlueQuickbarLink</span></h4>
==== addCologneBlueQuickbarLink ====
<p><code>utilities.addCologneBlueQuickbarLink(</code><i>URL</i><code>, </code><i>title</i><code>, </code><i>section</i> [<code>, </code><i>at start</i>]<code>)</code>
 
</p><p>Adds a link to the "quickbar" (the links to the left). <i>section</i> is the title of the section to add the link under. <i>at start</i> can be set to <code>true</code> to add the link to the top of the section, or <code>false</code> (or left off entirely) to add it to the bottom. If the section doesn't exist, it'll be added to the bottom.
utilities.addCologneBlueQuickbarLink(''URL'', ''title'', ''section'' [, ''at start''])
</p>
 
Adds a link to the "quickbar" (the links to the left). ''section'' is the title of the section to add the link under. ''at start'' can be set to true to add the link to the top of the section, or false (or left off entirely) to add it to the bottom. If the section doesn't exist, it'll be added to the bottom.
 
<pre>utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons", "Browse")
<pre>utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons", "Browse")
utilities.addMonobookNavboxLink("http://www.somesite.com/", "Some Site", "External Links")
utilities.addMonobookNavboxLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Some Site", "External Links")
</pre>
</pre>
 
<a id="addCologneBlueTopbarLink" name="addCologneBlueTopbarLink"></a><h4> <span class="mw-headline">addCologneBlueTopbarLink</span></h4>
==== addCologneBlueTopbarLink ====
<p><code>utilities.addCologneBlueTopbarLink(</code><i>URL</i><code>, </code><i>title</i> [<code>, </code><i>at start</i>]<code>)</code>
 
</p><p>Adds a link to the first top bar (the one with the Main Page, Log Out, etc links).
utilities.addCologneBlueTopbarLink(''URL'', ''title'' [, ''at start''])
</p>
 
<a id="addCologneBlueSubtopbarLink" name="addCologneBlueSubtopbarLink"></a><h4> <span class="mw-headline">addCologneBlueSubtopbarLink</span></h4>
Adds a link to the first top bar (the one with the Main Page, Log Out, etc links).
<p><code>utilities.addCologneBlueSubtopbarLink(</code><i>URL</i><code>, </code><i>title</i> [<code>, </code><i>at start</i>]<code>)</code>
 
</p><p>Adds a link to the second top bar (the one with the Printable Version, etc links).
==== addCologneBlueSubtopbarLink ====
</p>
 
<a id="TaviStyle-specific" name="TaviStyle-specific"></a><h3> <span class="mw-headline">TaviStyle-specific</span></h3>
utilities.addCologneBlueSubtopbarLink(''URL'', ''title'' [, ''at start''])
<p>These functions only work in the TaviStyle skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
 
</p>
Adds a link to the second top bar (the one with the Printable Version, etc links).
<a id="addTavistyleTopbarLink" name="addTavistyleTopbarLink"></a><h4> <span class="mw-headline">addTavistyleTopbarLink</span></h4>
 
<p><code>utilities.addTavistyleTopbarLink(</code><i>URL</i><code>, </code><i>title</i><code>, </code><i>row</i> [<code>, </code><i>at start</i>]<code>)</code>
=== TaviStyle-specific ===
</p><p>Add a link to the red links at the top of the page. Setting <i>row</i> to 1 will add it to the top row (Home, Forum, etc), 2 will add it to the second row (RecentChanges, etc). 3 will add a new row. <i>at start</i> can be set to <code>true</code> to add the link to the start of the row, or <code>false</code> (or left off entirely) to add it to the end.  
 
</p>
These functions only work in the TaviStyle skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.
<a id="addTavistyleBottombarLink" name="addTavistyleBottombarLink"></a><h4> <span class="mw-headline">addTavistyleBottombarLink</span></h4>
 
<p><code>utilities.addTavistyleBottombarLink(</code><i>URL</i><code>, </code><i>title</i><code>, </code><i>row</i> [<code>, </code><i>at start</i>]<code>)</code>
==== addTavistyleTopbarLink ====
</p><p>Add a link to the red links at the top of the page. Setting <i>row</i> to 1 will add it to the top row (Edit, talk, etc), 2 will add it to the second row (Your user page, your talk, etc). 3 will add a new row. <i>at start</i> can be set to <code>true</code> to add the link to the start of the row, or <code>false</code> (or left off entirely) to add it to the end.
 
</p>
utilities.addTavistyleTopbarLink(''URL'', ''title'', ''row'' [, ''at start''])
<a id="Examples" name="Examples"></a><h2> <span class="mw-headline">Examples</span></h2>
 
<a id="For_Monobook" name="For_Monobook"></a><h3> <span class="mw-headline">For Monobook</span></h3>
Add a link to the red links at the top of the page. Setting ''row'' to 1 will add it to the top row (Home, Forum, etc), 2 will add it to the second row (RecentChanges, etc). 3 will add a new row. ''at start'' can be set to true to add the link to the start of the row, or false (or left off entirely) to add it to the end.
<p>Add a couple of new links to the Quickbar:
 
</p>
==== addTavistyleBottombarLink ====
 
utilities.addTavistyleBottombarLink(''URL'', ''title'', ''row'' [, ''at start''])
 
Add a link to the red links at the top of the page. Setting ''row'' to 1 will add it to the top row (Edit, talk, etc), 2 will add it to the second row (Your user page, your talk, etc). 3 will add a new row. ''at start'' can be set to true to add the link to the start of the row, or false (or left off entirely) to add it to the end.
 
== Examples ==
 
=== For Monobook ===
 
Add a couple of new links to the Quickbar:
 
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage") // add it before the "userpage" link
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage") // add it before the "userpage" link
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Outta Here") // add it at the end
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Outta Here") // add it at the end
</pre>
</pre>
 
<p>Add a "Validate this page" tab:
Add a "Validate this page" tab:
</p>
 
<pre>utilities.addMonobookTab("<a class="external free" href="http://validator.w3.org/check?uri=" rel="nofollow" title="http://validator.w3.org/check?uri=">http://validator.w3.org/check?uri=</a>" + escape(utilities.getArticleURL()), "Validate", true)
<pre>utilities.addMonobookTab("http://validator.w3.org/check?uri=" + escape(utilities.getArticleURL()), "Validate", true)
</pre>
</pre>
 
<p>Add another row of tabs to the bottom of the screen:
Add another row of tabs to the bottom of the screen:
</p>
 
<pre>utilities.addMonobookBottomTabs()
<pre>utilities.addMonobookBottomTabs()
</pre>
</pre>
 
<a id="For_Cologne_Blue" name="For_Cologne_Blue"></a><h3> <span class="mw-headline">For Cologne Blue</span></h3>
=== For Cologne Blue ===
<p>Add a "You've got new messages" banner:
 
</p>
Add a "You've got new messages" banner:
 
<pre>if (utilities.cologneBlueNewMessages())
<pre>if (utilities.cologneBlueNewMessages())
{
{
   var div = document.createElement("div")
   var div = document.createElement("div")
   div.setAttribute("style", "background: #FFCE7B; border: 1px solid #FFA500; color: black; font-weight: bold; margin: 2em 0 1em; padding: 0.5em 1em");
   div.setAttribute("style", "background: #FFCE7B; border: 1px solid #FFA500; color: black; font-weight: bold; margin: 2em 0 1em; padding: 0.5em 1em");
   div.innerHTML = 'You have <a href="' + utilities.localurl("Username", 3) + '">new messages</a>.';     
   div.innerHTML = 'You have &lt;a href="' + utilities.localurl("<span style="font-family: sans-serif"><i>Username</i></span>", 3) + '"&gt;new messages&lt;/a&gt;.';     
   var article = document.getElementById('article');
   var article = document.getElementById('article');
   article.insertBefore(div, article.firstChild);
   article.insertBefore(div, article.firstChild);
}
}
</pre>
</pre>
 
<p>Add a couple of new links to the Quickbar:
Add a couple of new links to the Quickbar:
</p>
 
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Toons"), "Toons", "Browse") // add a Toons link to the "Browse" section
<pre>utilities.addMonobookQuickbarLink(utilities.localurl("Toons"), "Toons", "Browse") // add a Toons link to the "Browse" section
utilities.addMonobookQuickbarLink("http://www.homestarrunner.com/", "Official Site", "External links") // add a new "External links" section with this in it.
utilities.addMonobookQuickbarLink("<a class="external free" href="http://www.homestarrunner.com/" rel="nofollow" title="http://www.homestarrunner.com/">http://www.homestarrunner.com/</a>", "Official Site", "External links") // add a new "External links" section with this in it.
</pre>
</pre>
 
<p>Add "article" and "talk page" links to the top bar, like Monobook's tabs
Add "article" and "talk page" links to the top bar, like Monobook's tabs
</p>
 
<pre>var namespace = utilities.getArticleNamespace();
<pre>var namespace = utilities.getArticleNamespace();
var title = utilities.getArticleTitle();
var title = utilities.getArticleTitle();
Line 274: Line 210:
   if (utilities.articlenamespace[namespace] == namespace)
   if (utilities.articlenamespace[namespace] == namespace)
     pagelink.style.fontWeight = "bold";
     pagelink.style.fontWeight = "bold";
</pre>
<a id="For_TaviStyle" name="For_TaviStyle"></a><h3> <span class="mw-headline">For TaviStyle</span></h3>
<p>Add some new links to the top bar:
</p>
<pre>utilities.addTavistyleTopbarLink("<a class="external free" href="http://fanstuff.hrwiki.org/" rel="nofollow" title="http://fanstuff.hrwiki.org/">http://fanstuff.hrwiki.org/</a>", "Fanstuff", 1) // add the fanstuff to the top row
utilities.addTavistyleTopbarLink(utilities.localurl("Toons"), "Toons", 3) // add a third row, with an link to Toons
utilities.addTavistyleTopbarLink("<a class="external free" href="http://www.somesite.com/" rel="nofollow" title="http://www.somesite.com/">http://www.somesite.com/</a>", "Outta here", 3) // add an external link to the third row
</pre>
</pre>


=== For TaviStyle ===


Add some new links to the top bar:


<pre>utilities.addTavistyleTopbarLink("http://fanstuff.hrwiki.org/", "Fanstuff", 1) // add the fanstuff to the top row
utilities.addTavistyleTopbarLink(utilities.localurl("Toons"), "Toons", 3) // add a third row, with an link to Toons
<div class="visualClear"></div>
utilities.addTavistyleTopbarLink("http://www.somesite.com/", "Outta here", 3) // add an external link to the third row
</html>
</pre>
[[Category:Help]]



Latest revision as of 21:00, 18 July 2026

You can add JavaScript to any page, to let you customize how the page looks. If you use the Monobook skin (the default) you can put the Javascript here. If you use Tavistyle you can put it here, or if you use Cologne Blue you can put it here.

There are a lot of "helper" functions available. To use them, put {{subst:utilities}} at the top of this page. When you save, this will become

// include utility functions
document.write('<script type="text/javascript" src="/index.php?title=User:Phlip/utilities.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

which will include all the relevant code. If you want to stop using them, just remove those two lines.

They have been tested in Firefox and Internet Explorer (5.0, 5.5 and 6.0).

If you don't understand JavaScript, feel free to skip down to Examples and mix & match the pieces into the template in General.

General

You need to set up the code to be run when the page has loaded (otherwise it will try to edit a page that doesn't exist yet). Your JavaScript page should basically look like:

{{subst:utilities}}

function doeverything()
{
  // code goes here
}
if (window.addEventListener) window.addEventListener("load",doeverything,false);
else if (window.attachEvent) window.attachEvent("onload",doeverything);

and put the things below where it says // code goes here.

Available functions

All these functions are on an object called utilities. So to call them you need to type utilities.function(parameters).

Information

getArticleFullTitle

utilities.getArticleFullTitle()

Returns the title of the current page, like "Help:JavaScript".

alert("You're looking at " + utilities.getArticleFullTitle());

getArticleTitle

utilities.getArticleTitle()

Returns the title of the current page without the namespace, like "JavaScript". This is useful if you want to be able to find the associated talk page (see talknamespace)

getArticleNamespace

utilities.getArticleNamespace()

Returns the namespace of the current page. Note that this returns a magical number – to get the actual name, see namespaces below.

getArticleURL

utilities.getArticleURL()

Returns the URL for the page you're looking at. Note that this is a link to the article, not the exact page you're looking at – for example if you're editing a page, it will still link to the real page (not the edit page). If you want the full link, use window.location.

localurl

utilities.localurl(page [, namespace [, extras]])

Returns the URL of the given page. Again, the namespace should be a number. Examples:

utilities.localurl("Main Page")
utilities.localurl("User:Me") // this works
utilities.localurl("Me", 2)   // this works too
utilities.localurl("Me", 2, "action=edit") // an "edit" link for User:Me
utilities.localurl(utilities.getArticleTitle(), utilities.getArticleNamespace(), "action=edit") // an "edit" link for the current page

namespaces

utilities.namespaces[number]

An array of the names of each namespace. This is blank for the Main namespace, and the name for all the others.

var ns = utilities.getArticleNamespace();
if (ns == 0)
  alert("You're looking at the main namespace!");
else
  alert("You're looking at " + utilities.namespaces[ns] + "!");

namespacedescription

utilities.namespacedescription[number]

An array of the descriptions of each namespace – what the tabs are labeled with in Monobook. For example "Project Page" for a HRWiki namespace page, etc.

articlenamespace

utilities.articlenamespace[number]

An array of the article namespace for each talk namespace. For example 2 is "User" and 3 is "User talk", so utilities.articlenamespace[2] and utilities.articlenamespace[3] are both 2.

talknamespace

utilities.talknamespace[number]

An array of the talk namespace for each article namespace. For example 2 is "User" and 3 is "User talk", so utilities.talknamespace[2] and utilities.talknamespace[3] are both 3. utilities.talknamespace[-1] (the Special namespace) doesn't exist.

var ns = getArticleNamespace();
var talkpage = utilities.localurl(utilities.getArticleTitle(), utilities.talknamespace[ns]);

Monobook-specific

These functions only work in the Monobook skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.

addMonobookQuickbarLink

utilities.addMonobookQuickbarLink(URL, title [, where])

Adds a link to the "quickbar" (the bar with links to your user page, your talk page, logout, etc). "Where" should be one of:

  • "userpage" if you want the link to appear before your userpage link.
  • "mytalk" if you want the link to appear before your my talk link.
  • "preferences" if you want the link to appear before your preferences link.
  • "watchlist" if you want the link to appear before your my watchlist link.
  • "mycontris" if you want the link to appear before your my contributions link.
  • "logout" if you want the link to appear before your log out link.
  • Leave it off entirely if you want the link to appear after your log out link.
utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage")
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Outta Here")

addMonobookTab

utilities.addMonobookTab(URL, title, padding [, at start])

Adds a link to the tab bar (the bar with links to the page, the talk page, edit, history, etc). padding lets you group the tabs together (like edit and history are) – if it's set to true it will be separated from the other links, if you set it to false it won't be. at start says whether to add the tab to the start or to the end (the tab bar changes too much to be able to say "put it here"... some pages don't have an "edit" link if they're protected, Special pages only have the "special" tab...). If you leave at start off, it will put it on the end.

utilities.addMonobookQuickbarLink("http://www.somesite.com/", "A", true)  //  \
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "B", false) //   } these links will all be grouped together
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "C", false) //  /
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Loner", true) //  this one will start a new group

addMonobookNavboxLink

utilities.addMonobookNavboxLink(URL, title [, which one [, at start]])

Adds a link to the navigation boxes (the ones to the left). If which one is left off it will add to the "navigation" box. It can be set to:

  • "navigation" to add to navigation
  • "tb" to add to the toolbox
  • "ext" to add to external links

Again, at start can be true to add it to the top, or false (or leave it off) to add it to the bottom.

utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons")
utilities.addMonobookNavboxLink("http://www.somesite.com/", "Some Site", "ext")

addMonobookBottomTabs

utilities.addMonobookBottomTabs([no CSS])

Copies the tab bar to the bottom of the page. If you use addMonobookTab, this should come after those (so the new tabs will be copied too). Normally this adds a lot of extra CSS to the page to get it to work – if you'd rather add the CSS yourself to your custom CSS, then you can pass a true for no CSS – usually you can just leave it off entirely and it will work without extra effort.

Cologne Blue-specific

These functions only work in the Cologne Blue skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.

cologneBlueNewMessages

utilities.cologneBlueNewMessages()

Returns true if you nave new messages. The Cologne Blue "You have new messages" message isn't as "in your face" as the Monobook and TaviStyle ones are. This allows you to fix that, if you want.

if (utilities.cologneBlueNewMessages())
  alert ("Dude! Check your messages!");

addCologneBlueQuickbarLink

utilities.addCologneBlueQuickbarLink(URL, title, section [, at start])

Adds a link to the "quickbar" (the links to the left). section is the title of the section to add the link under. at start can be set to true to add the link to the top of the section, or false (or left off entirely) to add it to the bottom. If the section doesn't exist, it'll be added to the bottom.

utilities.addMonobookNavboxLink(utilities.localurl("Toons"), "Toons", "Browse")
utilities.addMonobookNavboxLink("http://www.somesite.com/", "Some Site", "External Links")

addCologneBlueTopbarLink

utilities.addCologneBlueTopbarLink(URL, title [, at start])

Adds a link to the first top bar (the one with the Main Page, Log Out, etc links).

addCologneBlueSubtopbarLink

utilities.addCologneBlueSubtopbarLink(URL, title [, at start])

Adds a link to the second top bar (the one with the Printable Version, etc links).

TaviStyle-specific

These functions only work in the TaviStyle skin – if you try to use them elsewhere you'll get a big obnoxious error message telling you to fix it.

addTavistyleTopbarLink

utilities.addTavistyleTopbarLink(URL, title, row [, at start])

Add a link to the red links at the top of the page. Setting row to 1 will add it to the top row (Home, Forum, etc), 2 will add it to the second row (RecentChanges, etc). 3 will add a new row. at start can be set to true to add the link to the start of the row, or false (or left off entirely) to add it to the end.

addTavistyleBottombarLink

utilities.addTavistyleBottombarLink(URL, title, row [, at start])

Add a link to the red links at the top of the page. Setting row to 1 will add it to the top row (Edit, talk, etc), 2 will add it to the second row (Your user page, your talk, etc). 3 will add a new row. at start can be set to true to add the link to the start of the row, or false (or left off entirely) to add it to the end.

Examples

For Monobook

Add a couple of new links to the Quickbar:

utilities.addMonobookQuickbarLink(utilities.localurl("Main Page"), "Back Home", "userpage") // add it before the "userpage" link
utilities.addMonobookQuickbarLink("http://www.somesite.com/", "Outta Here") // add it at the end

Add a "Validate this page" tab:

utilities.addMonobookTab("http://validator.w3.org/check?uri=" + escape(utilities.getArticleURL()), "Validate", true)

Add another row of tabs to the bottom of the screen:

utilities.addMonobookBottomTabs()

For Cologne Blue

Add a "You've got new messages" banner:

if (utilities.cologneBlueNewMessages())
{
  var div = document.createElement("div")
  div.setAttribute("style", "background: #FFCE7B; border: 1px solid #FFA500; color: black; font-weight: bold; margin: 2em 0 1em; padding: 0.5em 1em");
  div.innerHTML = 'You have <a href="' + utilities.localurl("Username", 3) + '">new messages</a>.';    
  var article = document.getElementById('article');
  article.insertBefore(div, article.firstChild);
}

Add a couple of new links to the Quickbar:

utilities.addMonobookQuickbarLink(utilities.localurl("Toons"), "Toons", "Browse") // add a Toons link to the "Browse" section
utilities.addMonobookQuickbarLink("http://www.homestarrunner.com/", "Official Site", "External links") // add a new "External links" section with this in it.

Add "article" and "talk page" links to the top bar, like Monobook's tabs

var namespace = utilities.getArticleNamespace();
var title = utilities.getArticleTitle();
if (utilities.talknamespace[namespace])
{
  var talkNS = utilities.talknamespace[namespace];
  var talklink = utilities.addCologneBlueSubtopbarLink(utilities.localurl(title, talkNS), utilities.namespacedescription[talkNS], true);
  if (utilities.talknamespace[namespace] == namespace)
    talklink.style.fontWeight = "bold";
}
var pageNS = utilities.articlenamespace[namespace]
var pagelink = utilities.addCologneBlueSubtopbarLink(utilities.localurl(title, pageNS), utilities.namespacedescription[pageNS], true);
  if (utilities.articlenamespace[namespace] == namespace)
    pagelink.style.fontWeight = "bold";

For TaviStyle

Add some new links to the top bar:

utilities.addTavistyleTopbarLink("http://fanstuff.hrwiki.org/", "Fanstuff", 1) // add the fanstuff to the top row
utilities.addTavistyleTopbarLink(utilities.localurl("Toons"), "Toons", 3) // add a third row, with an link to Toons
utilities.addTavistyleTopbarLink("http://www.somesite.com/", "Outta here", 3) // add an external link to the third row