A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

#ifexist module or function for JavaScript

Iniquity

Have we got smth like this? I want to detect a page.

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! :slight_smile:

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 :confused:

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:

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 :slight_smile:

Tgr

Filed T184953.