Have we got smth like this? I want to detect a page.
A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.
#ifexist module or function for JavaScript

Iniquity

Tgr
ResourceLoader/Core_modules is a good place to find out about useful things in Javascript. In this case, you are looking for
var title = new mw.Title( 'Foo' );
return title.exists();
(See also the documentation on mw.loader.using
if you are not familiar with how ResourceLoader works.)

Iniquity
Thanks for answer!

Iniquity
Hm, it is strange. title.exists() has alsways returned a null.

Tgr
Uh yeah, it looks like that function doesn’t actually do anything
You’ll probably have to do a normal API call then.

TheDJ
Correct, it can only retrieve that value if it already knows it, see also:
- https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Title-static-property-exist
- https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Title-method-exists
- https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Title-static-method-exists
you’ll want something like:
mw.loader.using( 'mediawiki.api' ).then( function() {
var api = new mw.Api();
api.get( {
titles: 'Foofafasfadf',
formatversion: 2
} ).then(function(data){
if( data.query.pages[0].missing ) {
console.log("missing page " + data.query.pages[0].title );
}
} );
} );
Try with the Sandbox as: https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&format=json&titles=Doesntexist|Main%20Page|Talk%3A&formatversion=2

Iniquity
Thanks! Its working

Tgr
Filed T184953.