Manual talk:Interface/JavaScript

Latest comment: 2 years ago by אני ואתה ואנחנו in topic Adding JS for CreateAccount page

File location

edit
  • Would you please tell me how do I install the Commons.js file into the MediaWiki? Where do I put this file? How do I activate it? Thank you in advance.

Enclosing with nowiki tags

edit

It's sometimes useful to start your Common.js with "// <nowiki>" and end it with "// "</nowiki> -- Sidcom 23:01, 22 February 2010 (UTC)Reply

why is that? --12.46.139.200 17:23, 22 August 2011 (UTC)Reply
Because some wikitext still gets parsed in your script file such as --~~~~. See topic below because I assume this is a bug. --Sebschlicht (talk) 20:51, 28 August 2014 (UTC)Reply

Not working in 1.16.2?

edit

Moved to Project:Support desk#Not working in 1.16.2?; further questions should go there. PleaseStand (talk) 04:24, 16 March 2012 (UTC)Reply

Saving of JS/CSS files

edit

If one edits a JS file and presses the save button, you will get redirected to the "Read"-view. This should not be the default behaviour for a JS file. This also applies to CSS files. Neither does one need a preview of the file itself, but of a wiki page using it. If the save button is clicked, one should stay at the page to do the next edit. Maybe I am missing a point, but at the moment I can't think of any setting where you would want to read the JS/CSS file after an edit. Does someone has an example I there are such settings? --Sebschlicht (talk) 20:51, 28 August 2014 (UTC)Reply

Following your same argumentation, when you edit a CSS/JS page and you hit save, you want the CSS/JS applied to the page, so you want to see a page (the one you just edited for example), and not stay on the same edit screen. Staying on the same edit screen will also be confusing, since you won't be certain if the edit was saved or it was rejected without an error. --Ciencia Al Poder (talk) 09:28, 29 August 2014 (UTC)Reply
When you edit a CSS/JS page and want to see a preview, you could enter the title of a page you want to see - with the new code applied. This functionality exists in the Lua editor, for example. You are right, one still needs to have feedback if you choose to save rather than to see a preview.
If the described preview functionality would exist for CSS/JS files you could always hit the preview button until you are satisfied. This not possible at the moment. --Sebschlicht (talk) 12:08, 25 September 2014 (UTC)Reply

Wikitext parsing

edit

If your JS file includes strings containing wikitext language such as --~~~~ it will get parsed and replaced as expected on wiki pages. This should not be the default behaviour in a JS file.

You can use

 // <nowiki>
 /* JS goes here */
 // </nowiki>

to overcome this issue. --Sebschlicht (talk) 20:51, 28 August 2014 (UTC)Reply

@Sebschlicht: Yeah, it was changed to be a separate content model from wikitext, but this apparently was left in for backwards compatibility (to avoid changing scripts and workflows) (phab:T34858). Some other aspects are potentially more useful in scripts, e.g. {{subst:}}. Mattflaschen-WMF (talk) 00:47, 4 January 2016 (UTC)Reply

Why database name is exposed via JS ?

edit

Considering page contents everyone can read database name used by site via mw.config.get('wgDBname'). This seems like security issue.

Why would it be? They still need to 1) contact the DB (shared database?), 2) know the DB username, 3) know the password. The DB name is not supposed to be sensitive. As for why it's public, the reason is that it's often a handy identifier for the website, e.g. "mediawikiwiki" is this wiki and not another wiki. Nemo 22:26, 3 January 2016 (UTC)Reply

JS to make "wgRevisionId" a magic word

edit

Hi,

I'm using Extension:Approved Revisions [1] and I have a need to be able to display the revision ID of the page revision currently being viewed. From the MW Manual page on Javascript [2] I see that the variable "wgRevisionId" contains this information and is exposed to mediawiki for use, but it is not clear to me how to do so. I would like to create a "magic word" [3] called {{VIEWEDREVID}} that outputs the revision ID of the revision being viewed such that it can be used in other parser logic

For example:

{{#ifexpr:( {{VIEWEDREVID}} < {{APPROVEDREVID}} )
|if true
|if false
}}

Can someone please help me identify the way to make a magic word that contains the value of "wgRevisionId"?

Thank you! -Rich User:revansx

[1] https://www.mediawiki.org/wiki/Extension:Approved_Revs

[2] https://www.mediawiki.org/wiki/Manual:Interface/JavaScript

[3] https://www.mediawiki.org/wiki/Help:Magic_words


Let's concentrate all discussions in one place: Extension talk:Approved Revs#Help making "wgRevisionId" a magic word --Ciencia Al Poder (talk) 09:09, 25 October 2018 (UTC)Reply

Missing values

edit

Why is this page missing values such as wgMonthNames and wgIllegalFileChars? Are they added by extensions, no longer supported, or something like that? BrandonXLF (talk) 01:17, 18 November 2019 (UTC)Reply

Is there a way to check if the page is being created?

edit

Is thre someting like wgRedlink or similar that I would be able to check to see if the page that is being edited is actually being created for the first time or it is just an empty page?Gone Postal (talk) 03:16, 26 January 2020 (UTC)Reply

mw.config.get('wgArticleId') will return 0 if the page doesn't exist yet. --Ciencia Al Poder (talk) 12:50, 26 January 2020 (UTC)Reply

Does a section name string exist?

edit

Apologies if I posted in the wrong section.

I'm currently using a script, Wikiplus, which allows an edit interface to appear without going to another page when I click "Quickedit" on any of the sections in an article. I'm trying to make the default summary point to the section name (and link it), but I can't seem to find a value that is similar to wgPageName but for sections. Does one exist? Tenryuu (talk) 00:14, 18 July 2020 (UTC)Reply

Adding JS for CreateAccount page

edit

Is it possible to run JS on the CreateAccount page through MediaWiki:Common.js? I tried and it doesn't work on that page, but it works on almost any other page, and I also checked the script on that page directly (through chrome's console) and it worked. אני ואתה ואנחנו (talk) 20:06, 2 July 2022 (UTC)Reply

Return to "Interface/JavaScript" page.