Check if file exists in Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • newcomer

    Check if file exists in Javascript

    Is there a way to check if a file exists in Javascript?
    This is what I'm trying to do:
    if(thisfile.htm exists)
    do this
    else
    do that
  • Lee

    #2
    Re: Check if file exists in Javascript

    newcomer said:[color=blue]
    >
    >Is there a way to check if a file exists in Javascript?
    >This is what I'm trying to do:
    >if(thisfile.ht m exists)
    > do this
    >else
    > do that[/color]

    Whether or not you can do something
    in Javascript
    in Javascript on a server
    in Javascript in a Windows Scripting Host shell
    in Javascript in all commonly used browsers
    or
    in Javascript in a particular version of a particular browser

    Are all different questions. Which do you mean?

    Comment

    • Paul Cooper

      #3
      Re: Check if file exists in Javascript

      On 4 Mar 2004 07:59:00 -0800, michellesmith13 4@hotmail.com (newcomer)
      wrote:
      [color=blue]
      >Is there a way to check if a file exists in Javascript?
      >This is what I'm trying to do:
      >if(thisfile.ht m exists)
      > do this
      >else
      > do that[/color]

      Client side, no there isn't under the standard security model. AFAIK
      it can be done with Internet Explorer provided the user allows an
      ActiveX control to run, but I certainly wouldn't!

      Paul

      Comment

      • Reply Via Newsgroup

        #4
        Re: Check if file exists in Javascript

        newcomer wrote:[color=blue]
        > Is there a way to check if a file exists in Javascript?
        > This is what I'm trying to do:
        > if(thisfile.htm exists)
        > do this
        > else
        > do that[/color]

        I do not believe it is possible - however you could cheat - there is a
        method to preload an image without actually displaying it - and one can,
        after the image has downloaded, check its size before displaying it.

        Thus... one could try and test for an html file as opposed to an image
        file - Give it a few seconds wait before checking the size of the html
        file and if its greater than zero, the page is likely to exist.

        I say likely in such that you might find that sometimes the page is
        being read from a proxy cache somewhere. One method around this might
        be to append arguements to your requested html file, like a time stamp.
        Thus, request whatever.html?1 3451324345 - This *might* mean that your
        request comes from the original server as opposed to a copy that might
        exist on a users proxy.

        There are alot of mights there - I'm a newbie with javascript, but I
        come from a Unix/Linux background which provides many solutions to a
        single problem - Its thought me to think this way with javascript.

        The following code is untested - again - I'm a newbie so someone else
        might be able to clean it up a little/lot...

        function checkIfRemoteFi leExists(fileTo Check)
        {
        var tmp=new Image;
        tmp.src=fileToC heck;
        if(tmp.complete )
        { window.alert(fi leToCheck+" is available"); }
        else
        { // Not got it yet - retry for a few seconds...
        window.setTimeo ut('checkIfRemo teFileExists(fi leToCheck)', 1000);
        }
        }

        checkIfRemoteFi leExists('http://www.where-ever.com/abc.html');

        I hope something above helps steer you in the right direction,
        randelld

        Comment

        Working...