How to enhance wiki content with JavaScript

As a security precaution to prevent attacks against the wiki, many HTML tags and all JavaScript is disabled in regular wikitext. This means that you cannot copy Google AdSense banners, Disqus comment boxes, Facebook like buttons, or any other embeddable objects into the source code of a page. You should also never allow any HTML or JavaScript to be useable in page source code because of the severe security vulnerabilities, which include allowing an attacker to gather literally every password used by all wiki users. By using JavaScript files in the system namespace and allowed HTML in pages, it is possible to add any HTML and JavaScript to wiki pages securely.

Creating the script
JavaScript programs can be written on the MediaWiki:Common.js page on your wiki. You will need to write JavaScript even if you only are using HTML because JavaScript is what will add the HTML elements to the page.

In case of HTML content, the script will contain the HTML you want to add and identify where to put it.

Use the following example to write your script.

Jquery is also shipped with MediaWiki core. XXX since what version? You can use a script like this:

When copying the script, change  to the identifier you will use inside your wiki page. For example, mw-unicornwiki-ads.

To add the HTML or JavaScript, change  to whatever HTML code that you want. Be careful about double and single quotes and use JavaScript escape characters where necessary.

Separate script
If your script gets too long or you want to keep snippets from a third-party library separate, you can create a new file in the MediaWiki namespace (ending in ), e.g.  . And then instruct MediaWiki to import that script. To do this, add an import script instruction to MediaWiki:Common.js:

importScript('MediaWiki:Example.js');

Enable on a wiki page
To begin adding the new object to wiki pages, create a template. For example, it could be called Template:Advertisement.

Add the following code to the template. Make sure the ID matches the identifier you specified in your script earlier.

You can use any tags or attributes to style the template and the content being added. Just make sure that you keep the identifier the same.

Enable it on a page by including the template on any wiki page(s) you want the script to apply: