www.webdeveloper.com
Results 1 to 4 of 4

Thread: javascript to check if url exist

  1. #1
    Join Date
    Nov 2007
    Posts
    3

    javascript to check if url exist

    <script language="JavaScript" type="text/javascript">
    <!--
    function login() {
    if (validLogin()) {
    passwd = document.form1.psword.value;
    location.href="./" temp_href="./"+passwd+".htm";

    }
    }


    function validLogin() {
    if (isBlank(document.form1.psword.value)){

    location.href="error.htm" temp_href="error.htm";

    return false;
    }
    checkIfRemoteFileExists(document.form1.psword.value);
    return true;
    }


    function checkIfRemoteFileExists(fileToCheck)
    {
    var tmp= new Image();

    tmp.src= fileToCheck + ".htm";

    window.alert(fileToCheck);

    // it never goes in the if part of the statement

    if(tmp.complete) // this is not working for some reason,
    {
    window.alert(tmp.complete+" is available");
    }
    else
    {
    document.form1.psword.value = "error";
    window.setTimeout('checkIfRemoteFileExists(fileToCheck)', 1000);
    window.alert(tmp.complete+" is not available");
    }
    }



    function isBlank(s) {
    return (s == "");
    }
    //-->
    </script>

  2. #2
    Join Date
    May 2005
    Location
    Northern Indiana
    Posts
    597
    I'm going to go out on a limb and say that
    Code:
    var tmp= new Image();
    tmp.src= fileToCheck + ".htm";
    is at least part of your problem. If you're going to create an image element, you need to point the source attribute to an actual image file, not an html document.

    If the file you're looking for is on a server you control, you're probably better off using an AJAX-esque approach to retrieve the contents of the file at the end of your URL, and then use a regex to help determine whether it is a real page, or your file-not-found error page.

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    As long as you are looking at your own domain you can use an AJAX HEAD request to check for a file.

    Code:
    function isThere(url) {
    	var req= new AJ(); // XMLHttpRequest object
    	try {
    		req.open("HEAD", url, false);
    		req.send(null);		
    		return req.status== 200 ? true : false;
    	}
    	catch (er) {
    		return false;
    	}
    }
    test case- substitute a path on your own domain:
    isThere(location.href);


    if you don't have an XMLHttpRequest object you can use this:

    Code:
    function AJ() {
    	var obj;
    	if (window.XMLHttpRequest) obj= new XMLHttpRequest(); 
    	else if (window.ActiveXObject){
    		try{
    			obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
    		}
    		catch(er){
    			try{
    				obj= new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(er){
    				obj= false;
    			}
    		}
    	}
    	return obj;
    }
    If the file you are checking is not on your domain you need to use a server script, like fcurl in php.

  4. #4
    Join Date
    Nov 2007
    Posts
    3

    I rewrote something Can you help me implement this.

    Quote Originally Posted by mrhoo
    As long as you are looking at your own domain you can use an AJAX HEAD request to check for a file.

    Code:
    function isThere(url) {
    	var req= new AJ(); // XMLHttpRequest object
    	try {
    		req.open("HEAD", url, false);
    		req.send(null);		
    		return req.status== 200 ? true : false;
    	}
    	catch (er) {
    		return false;
    	}
    }
    test case- substitute a path on your own domain:
    isThere(location.href);


    if you don't have an XMLHttpRequest object you can use this:

    Code:
    function AJ() {
    	var obj;
    	if (window.XMLHttpRequest) obj= new XMLHttpRequest(); 
    	else if (window.ActiveXObject){
    		try{
    			obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
    		}
    		catch(er){
    			try{
    				obj= new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(er){
    				obj= false;
    			}
    		}
    	}
    	return obj;
    }
    If the file you are checking is not on your domain you need to use a server script, like fcurl in php.
    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function login() {
    if (validLogin()) {
      passwd = document.form1.psword.value;
      location.href="./"+passwd+".htm";
    
    }
    }
    
    
    function validLogin() {
    if (isBlank(document.form1.psword.value)){
    
    location.href="error.htm";
    
    return false;
    }
    checkIfRemoteFileExists(document.form1.psword.value);
    return true;
    }
    
    // I have to change this function it is not used for a url. 
    //Say the person enters a password called cccccc1
    //then the file to check will be ccccccc1.htm in local directory of course 
    function checkIfRemoteFileExists(fileToCheck)
    {
    var tmp= new Image();
    
    tmp.src= fileToCheck + ".htm";
    
    window.alert(fileToCheck);
    
    if(tmp.complete) // It never goes in here even if the file exist in the directory.
    {
     //window.alert(tmp.complete+" is available");
    }
    else // it is always false
    {
     document.form1.psword.value = "error";
    }
    }
    
    
    
    function isBlank(s) {
    return (s == "");
    }
    //-->
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles