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 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    testing if file exist before doing a action

    Hello,

    I'm maintaining a application. And there is opportunity to open file's (like .doc, .jpg, .txt, etc.).
    Code:
      setFrame('frm_file', '/ALGEBESTANDEN/' + file_link);
    (The file_link is comming out from the database)

    But if the file not exist anymore, then I get a javascript error.

    Is there a simple opportunity to test if the file exist? It seems to me that this must be possible because I can the file open with Javascript as well, or...?

    thanks in advance,

    Nico
    Last edited by nkamp; 07-04-2006 at 12:43 PM.

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    function fileIntoFrame(strFile, strFrame) {
    	var objAJAX = null;
    	if(window.ActiveXObject) {
    		try {
    			objAJAX = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch(e) {
    			try {
    				objAJAX = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch(E) {
    				objAJAX = false;
    			}
    		}
    	} else if(typeof XMLHttpRequest != "undefined") {
    		objAJAX = new XMLHttpRequest();
    	}
    	
    	if(objAJAX) {
    		objAJAX.open("GET", strFile, true);
    		objAJAX.onreadystatechange = function() {
    			if(objAJAX.readyState == 4 && objAJAX.status == 200) { 
    				setFrame(strFrame, strFile); /* this uses your existing setFrame function */
    			}
    		}
    		objAJAX.send(null);
    	}
    }
    
    /* Usage */
    fileIntoFrame("/myFolder/myFile.txt", "myFrame");
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello Martin,

    Thank you for you're reaction.
    This is new to me so let's begin at the beginning.
    The first try is executed:
    Code:
    try {
             alert(window.ActiveXObject);
             objAJAX = new ActiveXObject("Msxml2.XMLHTTP");
    }
    But what is the meaning of this?

    Then:
    Code:
    alert(objAJAX) ;
     if(objAJAX) {
       objAJAX.open("GET", strFile, true);
    if(objAJAX) is a object! (I saw it with the alert). Did you expect this or did you expect true or false?

    What does objAJAX.open do?

    And on the end, the objAJAX.readystate is 4 and objAJAX.status is 200 even if the file doesn't exist. So on the end I have the same result as before.

    By the way the function setFrame is nothing more or les then:
    Code:
    function setFrame(framenaam, pagina) {
      parent[framenaam].location.href = pagina;
    }
    Nico

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    SImply put, it's an XMLHTTP call (w3schools ajax tutorial) that test the existance of the file before populating the frame with that file.

    Breaking down the script, the first part with the try .. catch statements is merely creating a cross-browser xml http object and assigning that to a variable (objAJAX). This is explained further in the above link.

    The xml http object has various methods and properties - again these are explained in more detail in the link, but essentially, the open() method of the cml http object is the bit that asks for the file. There are various http status code definitions - the best known being 404 (not found) wikipedia - 404 - but you only want to populate the frame if the file is found - which is http status 200. The readyState == 4 bit of is simply property of the xml http object that turns to 4 when loading is complete (if the page has been successfully loaded, or can't be found, or if there's another error, etc).

    If you want to read more about AJAX - then this is another fantastic resource/tutorial: Mozilla AJAX Tutorial - Getting started

    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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