-1

I need to check if a URL exists and redirect a user if it's not, we support various browsers (IE, Chrome, Firefox etc...) so the solution needs to be able to support all of those.

2
  • 1
    I need to check if a path exists What does that mean? A file path? A URL? Commented Jun 18, 2014 at 23:49
  • 1
    I need to check if a url exists Commented Jun 18, 2014 at 23:56

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

8 Comments

maybe I didnt explain myself correctly, I have clients accessing my website, I want to redirect to another location on my server (i.e. ..\MyOtherFolder\index.html) but first I want to verify that this relative url location exists, if it does, I want to redirect them to that location otherwise to a default one.
unfortunately, I never gets the readystate == 4, I have a partial url, that exists for a fact.
What is the file location you are attempting to check for? Please show directories and all.
I am using a relative path such as '..\Mobile\index.html'
Doesn't seems like. Mainly due to cross domain script attack prevention.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.