Talk:Adding javascript to wiki pages

From MediaWiki.org
Jump to navigation Jump to search

This howto is easy to understand also without js skill. but I had to change the js code (from MediaWiki:YourScript.js) as it is described here http://jsfiddle.net/7f9uko44/ (thanks to Krinkle from "mediawiki-l Mail-List"). I did the import code as recommended with importScript('MediaWiki:YourScript.js') (in MediaWiki:Common.js); and create a Template:HelloWorld and use that within pages but nothing happens. Do I forgot to activate or to allow js in mediawiki settings? In LocalSettings.php I set: $wgUseSiteJs = true; $wgAllowUserJs = true; $wgAllowUserCss = true; Is there any simple way to check js is running in my mediawiki? Ggtuem (talk) 14:16, 4 September 2014 (UTC)

why not support js newbies with a "hello world" howto? Ggtuem (talk) 10:57, 5 September 2014 (UTC)

Example Hello World!!![edit]

Here is a quick step-by-step for Hello World!!!:

Add to the following MediaWiki:Common.js:

(function () {

  var myElement = document.getElementById('mw-hello-world');
  myElement.innerHTML = '<html>Hello World!!!</html>';

}());

Create the page Template:Helloworld with the following:

<div id="mw-hello-world"></div>

Go to a regular page in the wiki and add the following:

{{Helloworld}}

You should see Hello World!!! on the page after saving. Nephron (talk) 02:05, 28 October 2016 (UTC)

importScript is now deprecated[edit]

importScript is now depricated
Please replace the code snippet under Separate Script

importScript('MediaWiki:Example.js');

with the updated version

mw.loader.load( '/w/index.php?title=MediaWiki:Gadget-HotCat.js&action=raw&ctype=text/javascript' );

See https://www.mediawiki.org/wiki/ResourceLoader/Legacy_JavaScript#importScript

Thanks.