Topic on Project:Support desk

How to check if a certain page exists using JavaScript

3
Guycn2 (talkcontribs)

Is there any way to check if a specific page exists, using JavaScript, and run a function only if it does?

Andrea.bozzano87 (talkcontribs)

Maybe you could perform a query on the database, searching for pages having titles similar to that page, or fetch the id from that page. Then you could implement a check, like:

result=query on the db where title like *page to look for*

if (result has rows)

{

it exists!run the function()

} else {

it doesnt exists!dont do nothing!

}

121.219.236.56 (talkcontribs)

Use the API.

 1 new mw.Api().get( {
 2     action: "query",
 3     titles: [ "This doesn't exist", "Main Page" ],
 4 } ).then( function( ret ) {
 5     $.each( ret.query.pages, function() {
 6         if ( this.missing !== "" ) {
 7             doTheThing();
 8         } else {
 9             dontDoTheThing();
10         }
11     } );
12 }, function( error ) {
13     dontDoTheThing();
14 } );
Reply to "How to check if a certain page exists using JavaScript"