Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the code below to check the status of a URL
JavaScript
var req = getReq();

try {
    req.open("GET", 'https://www.yahoo.com', false);
    //req.open("GET", 'http://myWebSite/' + s, false);
    req.send("");
} catch (e) {
    success = false;
    error_msg = "Error: " + e;
}
alert(req.status);

//function checkUrl(url) {
function getReq() {
    var req = false;
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e) {
            req = false;
        }
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            req = false;
        }
    }
    if (!req) {
        alert("Your browser does not support XMLHttpRequest.");
    }
    return req;
}

However, it ALWAYS return false. I am not sure why. Please help if you can. Thanks.

What I have tried:

Check the status of a URL - Not working using the code posted
Posted
Updated 23-Aug-18 9:40am

1 solution

Your code is going to fail on just about everything because no modern browsers support ActiveX controls any more. ActiveX turned out to be a huge security risk!
 
Share this answer
 
Comments
s yu 30-Aug-18 8:02am    
Actually, I did not use ActiveXObject. What I want to do is to use AJAX to check if the webpage for an image file (http://..../theImage.jpg) exists. If I use IE, I can check the xhr.status (e.g. 200 or 400). But for Chrome, I got error: "because its MIME type ('image/png') is not executable." Any idea how to handle it? Thanks.
Dave Kreskowiak 30-Aug-18 8:52am    
You can't do this is javascript in a browser. XmlHttpRequest cannot load a page from anything other than the domain the browser retrieved this page from.

So, if the browser got this code from a page on your site, www.mypage.com, it can only ever retrieve data from other pages (URL's) at www.mypage.com.

This is for security reasons.

If you want to get the status of a URL for another site, it MUST be done by the server-side code, not client-side javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900