Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problems checking for a valid page

    I am trying to use the script provided by someone in this forum. I seem to be missing something, but I'm not fluent enough to recognize what it is. Sorry about the duplicate previous post, I’m just learning how to navigate the site. Thanks for all of the help. Here is what I have so far:

    <form action="javascript:checkURL(document.getElementById('url').value)" >
    <input id="url" name="whatever" placeholder="./file.html">
    <input type="submit" >
    </form>


    <script>
    <!-- var xmlhttp;
    function checkURL(url){

    alert(url);

    xmlhttp = null; // initialize the request object
    // All the browsers except for the old IE
    if (window.XMLHttpRequest)
    {
    alert("step one")
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("HEAD",url,true);
    xmlhttp.send(null);
    }
    // old IE
    else if (window.ActiveXObject)
    {
    alert("step two")
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("HEAD",url,true);
    xmlhttp.send();
    }
    }
    }

    function xmlhttpChange()
    {
    // if loaded
    alert("step three");
    if (xmlhttp.readyState==4)
    {
    // if head exists "OK"
    if (xmlhttp.status==200)
    {
    alert('URL exists');
    }
    else
    {
    alert("Status is "+xmlhttp.status);
    }
    }
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    try replacing HEAD in
    Code:
    xmlhttp.open("HEAD",url,true);
    with GET or POST and check again...

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Tried your suggestions

    Well I tried both get and post with no results. It gets to step one only. There are no errors and script just ends...

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    what value you are providing in the url ???

  • #5
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am looking for a local page such as line no 2 "./filename.html'. If the page doesn't exhist I will branch to a custom 404 error page...

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Well, ie6 has been banned by google
    and most of the modern world.
    So why not do it like this ....

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    
    <script type="text/javascript">
    
    var xmlhttp = null;
    
    function checkURL(url){	 
    	if (window.XMLHttpRequest){		
    		xmlhttp = new XMLHttpRequest();
    		xmlhttp.onreadystatechange=xmlhttpChange;
    		xmlhttp.open("HEAD",url,true);
    		xmlhttp.send(null);
    	}else{
                              var td1 = document.getElementById("td1");
    		td1.innerHTML = "Your Browser has been Banned!";
                 }	
    }		
    function xmlhttpChange(){
     	if (xmlhttp.readyState == 4){
    		var td1 = document.getElementById("td1");
    		td1.innerHTML = "MSN OK";
    	}
    }
    </script>
    
    </head>
    <body>
    <div id="td1">
    <p><input type="button" onclick=checkURL("http://www.msn.com")
     value="Check MSN"></p>
    </div>
    </body></html>
    Last edited by DaveyErwin; 09-17-2011 at 10:02 PM.

  • #7
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the sample script. It did allot for pointing the way to solving my problem. There is one flaw in your script. The (xmlhttp.readyState == 4) will always return a value of 4. It specifies that the check for readyState check is complete. It will run 3 to 4 times and always return true. The key to validating the web page is in (xmlhttp.status), which returns either 200 (page exists) or 404 (page not found). I did find that this script will not verify any local web pages. The result is code '123' for any file selected. The next thing I tried was to call the file from the internet which works. I finally have the script working except when I call the function to check the status I loose the variables for url, page and redalert. The response I get is undefined. Is there any way that I can pass these values?

    The script works fine for files that error with code = 404 when I use a simple if statement without the else clause. When I add the else clause nether branch works regardless of if the file is there or not. All I get is two alerts = undefined and links to’ page not found’…

    The script:

    You can enter the golfers name or find it in the index above.<BR></B>
    eg; Bill Smith Sr<BR><BR>
    <form action="javascript:doSomethingWith(document.getElementById('golfname').value);" >
    <input id="golfname" name="whatever" placeholder="eg; Bill Smith">
    <input type="submit" >
    <script>
    function doSomethingWith(redalert){
    var lab = "";
    var result = redalert.toUpperCase();
    var word=result.split(" ");
    fname = word[0];
    lname = word[1];
    lab = fname.concat("_");
    lab = lab.concat(lname);
    if(word[2]){
    ext = word[2];
    lab = lab.concat("_");
    lab = lab.concat(ext);
    }
    var url = "";
    url = url.concat(lab);
    url = url.concat("_scores.html");

    var page = "./Golfers/"
    page = page.concat(url)
    var addpre = "http://marrgolf.com/Golfers/"
    url = addpre.concat(url)
    if (window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("HEAD",url,true);
    xmlhttp.send(null);
    }else{
    var td1 = document.getElementById("td1");
    td1.innerHTML = "Your Browser has been Banned!";
    }
    }

    function xmlhttpChange(page){

    if (xmlhttp.readyState == 4 && xmlhttp.status == 404){
    alert(xmlhttp.status)
    var newpage = "./Golfer_error.html"
    window.location.replace(newpage)
    }else{
    alert(page)
    window.location.replace(page)
    }
    }

    </script>

  • #8
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the sample script. It did allot for pointing the way to solving my problem. There is one flaw in your script. The (xmlhttp.readyState == 4) will always return a value of 4. It specifies that the check for readyState check is complete. It will run 3 to 4 times and always return true. The key to validating the web page is in (xmlhttp.status), which returns either 200 (page exists) or 404 (page not found). I did find that this script will not verify any local web pages. The result is code '123' for any file selected. The next thing I tried was to call the file from the current internet website, which works. I finally have the script working except when I call the function to check the status I loose the variables for url, page and redalert. I would like to send the golfers name "redalert" to the error page and link to the requested page 'page'. The response I get is undefined. Is there any way that I can pass these values?

    The script works fine for files that error with code = 404 when I use a simple if statement without the else clause. When I add the else clause nether branch works regardless of if the file is there or not. All I get is two alerts = undefined and links to’ page not found’…

    The script:

    <BR><BR>
    You can enter the golfers name or find it in the index above.<BR></B>
    eg; Bill Smith Sr<BR><BR>
    <form action="javascript:doSomethingWith(document.getElementById('golfname').value);" >
    <input id="golfname" name="whatever" placeholder="eg; Bill Smith">
    <input type="submit" >
    <script>

    <!-- This first section of code converts the name to all uppercase chars and breaks
    <!-- the name down to words. Since I don't know how to replace charactors in JavaScript
    <!-- I reassembled the name with under scores and added the final end to the web address
    <!-- which is url.

    function doSomethingWith(redalert){
    var lab = "";
    var result = redalert.toUpperCase();
    var word=result.split(" ");
    fname = word[0];
    lname = word[1];
    lab = fname.concat("_");
    lab = lab.concat(lname);
    if(word[2]){
    ext = word[2];
    lab = lab.concat("_");
    lab = lab.concat(ext);
    }
    var url = "";
    url = url.concat(lab);
    url = url.concat("_scores.html");

    <!-- The golfers are in a subdirectory so I added the path name to the url

    var page = "./Golfers/";
    page = page.concat(url);

    <!-- 'Page' is the path and file name of the page to retreive
    <!-- Now that we have the url of the page we need to find the internet address

    var addpre = "http://marrgolf.com/Golfers/";
    url = addpre.concat(url);

    <!-- Now that I have the requested internet address of the golfers page, 'url', I now need
    <!-- to know if it really exists. We don't want the client to get a 'page not found',
    <!-- but to tell him it is not in our database and return him to the Golfers index page.

    if (window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("HEAD",url,true);
    xmlhttp.send(null);
    }else{
    var td1 = document.getElementById("td1");
    td1.innerHTML = "Your Browser has been Banned!";
    }
    }

    function xmlhttpChange(page){

    if (xmlhttp.readyState == 4 && xmlhttp.status == 404){
    var newpage = "./Golfer_error.html"
    window.location.replace(newpage)

    <!-- Up to this point the code works a expected. When I tried to ad the else clause
    <!-- every thing when to pot. nothing works right!
    <!-- }else{
    <!-- alert(page)
    <!-- window.location.replace(page)
    }
    alert(page)
    }

    </script>

  • #9
    New Coder
    Join Date
    Sep 2011
    Location
    Wisconsin
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks everyone for your help. I finally resolved my problem. I defined my variables before the called function, which makes them useable in all functions. I didn't realize that the script is scanned from top to bottom to find the function to execute. There was a secondary problem. I had redefined 'page' in the first function call by 'var page' which cleared the global variable. I resolved the 'if else' problem by using two separate if statements. The only problem I am left with is this script works fine in FireFox but doesn't work at all in IE8...

    Final script :

    <script type="text/javascript">
    <!-- This first section of code converts the name to all uppercase chars and breaks
    <!-- the name down to words. Since I don't know how to replace charactors in JavaScript
    <!-- I reassembled the name with under scores and added the final end to the web address
    <!-- which is url.
    var page = "blank";
    var redalert = "";
    var fullname = "";
    function doSomethingWith(redalert){
    var lab = "";
    fullname = redalert
    var result = redalert.toUpperCase();
    var word=result.split(" ");
    fname = word[0];
    lname = word[1];
    lab = fname.concat("_");
    lab = lab.concat(lname);
    if(word[2]){
    ext = word[2];
    lab = lab.concat("_");
    lab = lab.concat(ext);
    }
    var url = "";
    url = url.concat(lab);
    url = url.concat("_scores.html");

    <!-- The golfers are in a subdirectory so I added the path name to the url
    var qm = "?"
    page = "./Golfers/";
    page = page.concat(url);
    <!-- page = Page.concat(qm);
    <!-- page = page.concat(fullname);

    <!-- 'Page' is the path and file name of the page to retreive
    <!-- Now that we have the url of the page we need to find the internet address

    var addpre = "http://marrgolf.com/Golfers/";
    url = addpre.concat(url);

    <!-- Now that I have the requested internet address of the golfers page, 'url', I now need
    <!-- to know if it really exista. We don't want the client to get a 'page not found',
    <!-- but to tell him it is not in our database and return him to the Golfers index page.

    if (window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("HEAD",url,true);
    xmlhttp.send(null);
    }else{
    var td1 = document.getElementById("td1");
    td1.innerHTML = "Your Browser has been Banned!";
    }
    }

    function xmlhttpChange(){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
    <!-- window.location.replace(page)
    window.location = page
    }
    if (xmlhttp.readyState == 4 && xmlhttp.status == 404){
    window.location = "./Golfer_error.html"
    }
    }
    </script>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •