What MediaWiki:Importjs does and when you need it

MediaWiki:Importjs is a special page on a MediaWiki site that loads JavaScript code automatically for every user who visits. Instead of editing individual user JavaScript files or adding scripts one at a time, you create this single page and write the code you want to run site-wide. When someone loads any page on your wiki, the code in MediaWiki:Importjs runs in their browser.

You need this if you want to add custom functionality that affects all users — things like custom buttons in the editor, automated formatting, page alerts, or modifications to how the interface looks. Without MediaWiki:Importjs, each user would have to manually add the code to their own user JavaScript file, which is impractical for a shared site.

The process requires administrator access to your wiki. If you do not have that permission, you will need to ask a site administrator to create or edit the page for you.

Key Takeaways

  • MediaWiki:Importjs is a special wiki page that runs JavaScript code for every user automatically, without them having to do anything.
  • You must have administrator rights to create or edit MediaWiki:Importjs; regular users cannot modify it.
  • The page is created like any other wiki page, but the code inside must be valid JavaScript or it will break the site interface for all users.
  • Test your code in a development or sandbox wiki before adding it to a live MediaWiki:Importjs page.
  • Code in MediaWiki:Importjs runs after the page loads, so it may not work on elements that load after the script finishes.

How to access and create the MediaWiki:Importjs page

Log in to your wiki with an administrator account. In the search box at the top of any page, type MediaWiki:Importjs exactly as written, with a capital M and capital I. Press Enter or click the search button.

If the page does not exist yet, the search results will show a red link that says "MediaWiki:Importjs" with text underneath saying the page does not exist. Click that red link. A blank edit page will open. If the page already exists, you will land on the page itself; click the Edit tab at the top to open it for editing.

You are now in the edit interface. The text box is where you will paste or write your JavaScript code. Do not add wiki markup or HTML tags — paste only the raw JavaScript.

Writing and pasting JavaScript into MediaWiki:Importjs

JavaScript code in MediaWiki:Importjs should be wrapped in a comment block to prevent errors. Start with /* <nowiki> */ on the first line and end with /* </nowiki> */ on the last line. The <nowiki> tags tell MediaWiki not to process the code as wiki syntax, and the comment markers prevent the tags from breaking the JavaScript.

Between those lines, paste your JavaScript code. A simple example that adds a message to the browser console looks like this:

/* <nowiki> */ console.log("MediaWiki site loaded"); /* </nowiki> */

If you are loading code from an external file or another wiki page, you can use the mw.loader.load() function. For example, to load a script from your wiki's common.js page, you would write:

/* <nowiki> */ mw.loader.load( '/w/index.php?title=MediaWiki:Common.js&action=raw&ctype=text/javascript' ); /* </nowiki> */

Always test code in a sandbox or development wiki first. A syntax error in MediaWiki:Importjs can break the interface for all users on your site.

Saving your changes and checking for errors

After you paste your code, scroll down to the edit summary box. Write a brief note about what you changed — for example, "Added custom button to editor toolbar" or "Loaded external script library". This helps other administrators understand what the page does.

Click the Publish Changes button (or Save Page, depending on your MediaWiki version). The page will save and the code will begin running for all users immediately.

Open a new browser tab and visit any page on your wiki. Open the browser's developer console (usually F12 or right-click and select Inspect) and look at the Console tab. If your code ran without errors, you will see any messages you programmed it to display. If there are red error messages, the code has a syntax problem. Go back to MediaWiki:Importjs, find the error, and fix it.

Common mistakes and how to avoid them

The most frequent error is forgetting the <nowiki> tags or the comment markers. Without them, MediaWiki will try to interpret parts of your code as wiki syntax and break it. Always use the format shown above.

Another common issue is code that runs before the page is fully loaded. If your script tries to modify an element that does not exist yet, it will fail silently. Wrap your code in a function that waits for the page to load, using mw.hook( 'wikipage.content' ).fire() or $( document ).ready().

Do not paste code you do not understand. Malicious or poorly written JavaScript can slow down the wiki, expose user data, or break the interface. If you are not sure what a script does, test it in a sandbox first or ask another administrator to review it.

When MediaWiki:Importjs does not work as expected

If you add code to MediaWiki:Importjs and nothing happens, the code may not be running at all. Check the browser console for errors. If there are no errors but the code still does not work, the code might be running before the page elements it needs are loaded. Wrap your code in a check that waits for those elements to exist.

If the code runs but produces the wrong result, the issue is usually with the code itself, not with MediaWiki:Importjs. Test the code in your browser's console on a wiki page to see what it actually does. You can also add console.log() statements throughout your code to see which parts are running.

If your code breaks the wiki interface for all users, you can still fix it. Log in as an administrator, go back to MediaWiki:Importjs, and either remove the problematic code or comment it out by adding // at the start of each line. Save the page and the interface will return to normal.

Frequently Asked Questions

Do I need to reload the page for changes to MediaWiki:Importjs to take effect?

Yes. After you save changes to MediaWiki:Importjs, you need to do a hard refresh in your browser — usually Ctrl+Shift+R on Windows or Cmd+Shift+R on Mac. A regular refresh may load the old cached version. Other users will see the new code on their next page load.

Can I load JavaScript from outside my wiki using MediaWiki:Importjs?

You can, but it is not recommended for security reasons. If you load code from an external site and that site is compromised, the malicious code runs on your wiki for all users. If you need external libraries, download them and host them on your wiki instead, or use libraries that MediaWiki already includes.

What is the difference between MediaWiki:Importjs and a user's personal JavaScript file?

MediaWiki:Importjs runs for every user automatically. A user's personal JavaScript file (like User:Username/common.js) runs only for that one user, and only if they have JavaScript enabled in their preferences. Use MediaWiki:Importjs for site-wide features and user files for personal customizations.

Will MediaWiki:Importjs slow down my wiki?

It depends on how much code you add and how complex it is. Simple code has no noticeable effect. Large scripts or code that runs loops on every page load can slow things down. Monitor your wiki's performance and remove or optimize code that causes delays.

Can I use MediaWiki:Importjs to load code from another wiki page?

Yes. Use mw.loader.load() with the full URL to the page's raw view. For example, to load code from MediaWiki:Common.js on the same wiki, use the URL format shown in the writing section above. You can also load from other wikis if you use their full domain name in the URL.