What MediaWiki:ImportJS actually does

MediaWiki:ImportJS is a page on a MediaWiki site that runs custom JavaScript code when someone visits any page on that wiki. It is not something you "create" in the sense of building from scratch — it is a special system page that already exists on every MediaWiki installation. What you do is edit it to add your own code.

Think of it as a master control panel. When you put JavaScript into MediaWiki:ImportJS, that code runs for every user on every page, every time. This is different from putting code in your own user JavaScript file, which only affects your own account. MediaWiki:ImportJS affects the whole site.

Only administrators can edit MediaWiki:ImportJS. If you do not have admin rights, you cannot change it. If you are an admin and you make a mistake in the code, you can break the site for everyone, so this is not a place to experiment.

Key Takeaways

  • MediaWiki:ImportJS is a system page that runs JavaScript for all users on all pages — you edit it, not create it from scratch.
  • Only wiki administrators can edit MediaWiki:ImportJS, and mistakes in the code will affect every visitor to the site.
  • You reach it by typing "MediaWiki:ImportJS" directly into the search bar or by navigating to it as you would any other page.
  • The code you add runs after the page loads, so it can modify the page, add buttons, or change how things behave.
  • If you are not an admin or you are unsure about JavaScript, ask your wiki administrator to make the change instead.

How to find and access MediaWiki:ImportJS

On your wiki, go to the search box at the top of any page. Type MediaWiki:ImportJS exactly as written — capital M, capital W, capital I, capital J, capital S. Press Enter or click the search button.

The page will load. If the page is empty or nearly empty, that means no custom JavaScript has been added yet. If there is already code on the page, that is code someone else added before you. You will see an Edit button (usually labeled "Edit" or "Edit source") at the top right. Click it to open the editor.

If you do not see an Edit button, you do not have administrator rights on this wiki. Contact the person who runs the wiki and ask them to add the code for you, or ask them to give you administrator access if you need to make changes regularly.

What kind of code goes in MediaWiki:ImportJS

MediaWiki:ImportJS runs JavaScript, which is a programming language that runs in web browsers. The code you put here can do things like add new buttons to the toolbar, hide certain page elements, change colors, add custom menus, or run actions when someone clicks something.

A straightforward example: if you wanted to add a button that appears on every page, you would write JavaScript code that creates that button and tells it what to do when someone clicks it. If you wanted to hide the sidebar on mobile devices, you would write code that detects the screen size and hides the sidebar accordingly.

The code must be valid JavaScript. If you write code with errors — a missing semicolon, a misspelled function name, or a bracket in the wrong place — the code will not run and may cause errors in the browser console. Other code on the page might stop working too.

The difference between MediaWiki:ImportJS and other JavaScript locations

MediaWiki has several places where you can put JavaScript, and they work differently. MediaWiki:ImportJS runs for everyone on every page. Your personal User:YourUsername/common.js file runs only for you, on every page. Individual pages can have their own JavaScript in their source code.

If you want to test code before it affects the whole site, write it in your own User:YourUsername/common.js first. That way, only you see it. Once you know it works, you can move it to MediaWiki:ImportJS so everyone sees it.

Some wikis also use MediaWiki:Common.js, which is similar to MediaWiki:ImportJS but is older and less commonly used on new installations. Check with your wiki administrator about which one your site uses.

How to add code safely

Before you edit MediaWiki:ImportJS, make a backup. Copy all the code that is already there and paste it into a text file on your computer. If something goes wrong, you can paste it back.

Add your new code at the bottom of the page, after any code that is already there. Do not delete or change existing code unless you know exactly what it does. Add a comment above your code explaining what it does — in JavaScript, comments start with // on a single line or /* */ for multiple lines.

After you paste your code, scroll down and click the Save button. The page will reload. Open a new browser tab and visit a page on your wiki. Open the browser console (usually Ctrl+Shift+J on Windows or Cmd+Option+J on Mac) and look for error messages. If you see red errors, your code has a problem.

If something breaks, go back to MediaWiki:ImportJS, delete the code you added, and save again. The site should return to normal. Then figure out what went wrong before you try again.

When you should not use MediaWiki:ImportJS

Do not use MediaWiki:ImportJS to load external JavaScript files from other websites. This is a security risk — if that other website is hacked or goes down, it can break your wiki or expose your users to malicious code.

Do not use it to store sensitive information like passwords or API keys. Anyone who can view the page source can see that code, and it will be sent to every visitor's browser.

Do not use it if you are not confident in your JavaScript skills. A small mistake can slow down the site for everyone or break features that other people depend on. If you are learning, test your code in your own User:YourUsername/common.js file first, or ask someone experienced to review it before you add it to MediaWiki:ImportJS.

Common reasons to use MediaWiki:ImportJS

Many wikis use MediaWiki:ImportJS to add features that the base MediaWiki software does not include. Some examples: adding a custom search box, hiding the edit button for anonymous users, adding a notification banner at the top of every page, or changing how the table of contents looks.

Some wikis use it to load a library of code that other pages or templates depend on. For example, if you have a template that needs special JavaScript to work, you might load that code in MediaWiki:ImportJS so the template works everywhere.

Other wikis use it to track user behavior or integrate with external services — though again, be careful with security and privacy when you do this.

Frequently Asked Questions

Can I edit MediaWiki:ImportJS if I am not an admin?

No. Only administrators can edit system pages like MediaWiki:ImportJS. If you need a change made, contact your wiki administrator and describe what you want the code to do. They can write it or ask someone who knows JavaScript to write it.

What happens if I break the code in MediaWiki:ImportJS?

If your code has errors, it may prevent other JavaScript from running or cause the page to load slowly. Visitors will see errors in their browser console. You can fix it by going back to MediaWiki:ImportJS, finding the broken code, and either fixing it or deleting it, then saving again.

Does MediaWiki:ImportJS run before or after the page loads?

It runs after the page loads. This means your code can find and modify elements that are already on the page. If you need code to run before the page displays, you would use a different method, which your administrator can explain.

Can I use MediaWiki:ImportJS to add CSS styling?

You can use JavaScript to add CSS, but if you only want to change colors or fonts, use MediaWiki:Common.css instead. That is the proper place for styling. MediaWiki:ImportJS is for code that needs to run and do things, not just change how things look.

Is there a way to test code before putting it in MediaWiki:ImportJS?

Yes. Write your code in your own User:YourUsername/common.js file first. Only you will see it run. Once you know it works, you can ask an administrator to copy it to MediaWiki:ImportJS, or you can copy it yourself if you have admin rights.