Dantman/Tricks
< Dantman
Contents |
Some built-in functions
Edit
I found your tricks page and was wondering if you knew about these built-in functions in MediaWiki?
These are what I use to import/append CSS or Javascript to pages.
Background
Edit
A handy collection of javascript functions called wikibits.js is included in everypage on Wikipedia. On Wikia these functions are found in allinone_non_loggedin.js (this file is loaded on every page) which is compressed (not readable), to see the readable code it says you must add query string ?allinone=0 at the end. I haven't gotten that part to work, but the functions are there. How I found these is because I am web developer and I study webpages very carefully. ;)
Functions
Edit
Using Wikipedia's wikibits.js, I'll list some of the useful functions:
- function addOnloadHook(hookFunct)
- function importScript(page)
- function importStylesheet(page)
- function importStylesheetURI(url)
- function appendCSS(text)
- function escapeQuotes(text)
- function escapeQuotesHTML(text)
- function addHandler( element, attach, handler )
- function addClickHandler( element, handler )
- function removeHandler( element, remove, handler )
Examples
Edit
These would go in Common.js:
Code #1
Edit
function loadFunc()
{
//your code here
}
addOnloadHook(loadFunc);
Result #1
Edit
Your function will run after the page as loaded. Equivalent to having window.onload=yourFunc; in the header.
Code #2
Edit
This is a snippet from my wiki that uses the Yahoo! User Interface library of scripts.
var container = 'http://yui.yahooapis.com/2.6.0/build/container/container-min.js'; var container_css = 'http://yui.yahooapis.com/2.6.0/build/container/assets/skins/sam/container.css'; importScriptURI(container); importStylesheetURI(container_css);
Result #2
Edit
This will import an external javascript or style sheet.
Code #3
Edit
appendCSS(".notes {font-size:10px;}");
Result #3
Edit
This will find the <head> tag of the webpage and dynamically create a <style> tag and insert your CSS.
Other wikis
Edit
- Memory Alpha's Common.js uses the hookEvent()
- Everyquest's Common.js uses addOnloadHook()
- WOWWiki's Common.js also use addOnloadHook()
Hope this may be useful!
I think will I have to start my own tricks page now if no one knows about this stuff :)
— JeiaraTalk 18:47, 24 March 2009 (UTC)
Namespace and Group information
Edit
There is the API.--AB 15:06, October 24, 2009 (UTC)