How to Check If Page Exists Using JavaScript

Checking if a URL exists or not using javascript

You can't.

Browsers won't let arbitrary websites the visit use them to probe other websites.

You can only do this using server side code (which you can use to pass information to the browser via Ajax).

How to check if page exists using Javascript?

I'm using Javascript and i want check if a page/url (of another domain
exist)

If only requirement is to check if the external resource is available, one possible workaround for No 'Access-Control-Allow-Origin' header is present on the requested resource. response from AJAX is to instead request .html document or url at src of <img> element. Check onerror handler for GET http://example.com/ net::ERR_NAME_NOT_RESOLVED

var img = new Image;
img.onload = function(e) {
console.log(this, this.src)
}

img.onerror = function(e) {
console.log(e, this)
};

img.src = "http://example.com";

Jquery - Check to see if page exists

You can do this:

<a href="/somelink" id="test1">Link1</a> <span id="result1"></span>
$.ajax($("#test1").attr("href"), {
statusCode: {
404: function() {
$("#result1").html("not working");
},
200: function() {
$("#result1").html("working");
}
}
});

Checking if a webpage exists on the domain, otherwise load another page

Alright, so thanks to everyone who commented as they guided me to the solution: the problem was the configuration of the web host. I am hosting the code on Firebase and firebase.json was configuring what would happen in case a page wasn't found i.e. redirecting to index.html. I removed that rewrite and the code works as intended.

I did want to try and be more subtle, to keep that default behaviour for all pages not inside /photos/ nor photos.html (which contains the code mentioned above), but I must be doing something wrong still – but that's for another topic! Thanks again to all the commenters.

"rewrites": [
{
// Excludes specified pathways from rewrites
"source": "!/photos/**",
"destination": "/index.html"
}
, {
// Excludes specified pathways from rewrites
"source": "!/photos.html",
"destination": "/index.html"
}
,
{
// If it makes it here, it didn't match any previous routing
// Serves index.html for requests to files or directories that do not exist
"source": "**",
"destination": "/index.html"
}

How to Check if URL exists using javascript

In the header of your page, place this javascript code:

        <script type="text/javascript">

// Creates an object which can read files from the server
var reader = new XMLHttpRequest();

var checkFor = "fileToCheckFor.html";

// Opens the file and specifies the method (get)
// Asynchronous is true
reader.open('get', checkFor, true);

//check each time the ready state changes
//to see if the object is ready
reader.onreadystatechange = checkReadyState;

function checkReadyState() {

if (reader.readyState === 4) {

//check to see whether request for the file failed or succeeded
if ((reader.status == 200) || (reader.status == 0)) {

//page exists -- redirect to the checked
//checked for page
document.location.href = checkFor;

}
else {

//does nothing and quits the function
//if the url does not exist
return;

}

}//end of if (reader.readyState === 4)

}// end of checkReadyState()

// Sends the request for the file data to the server
// Use null for "get" mode
reader.send(null);

</script>
This allows you to check whether or not a page exists on your server and redirect to it if it does. If the page does not exist, the javascript code does nothing and allows the current page to load.

Edit: Fixed bugs and rewrote some of the code for clarity, appearance, and practicality.

How can I check to see if a URL exists or not?

You can use Node's https built-in module to determine the status code of the page. If the status code is 200, you would know the page exists. You could also check for other status codes (404, like you mentioned) that you see fit.

const https = require('https')

https.get('https://discordapp.com/', res => {
if(res.statusCode === 200) {
console.log('Page exists!')
}
})

Error when attempting to check if page exists or not (function returns undefined)

The return value of checkURL will be undefined until the AJAX request finishes. I would approach this with a callback like this:

function checkURL(url, cb) {
var request = new XMLHttpRequest();
request.open('GET', url, true);

request.onload = function() {
if (request.status >= 200 && request.status < 400) {
cb(true)
} else {
cb(false)
}
};

request.onerror = function() {
cb(false)
};

request.send();
}

checkURL('https://www.reddit.com/r/javascript.json', function(status) {
if (status) {
// did not 404
} else {
// 404 or error
}
})


Related Topics



Leave a reply



Submit