Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Javascript file existing checking [modified]

Javascript file existing checking [modified]

Scheduled Pinned Locked Moved Web Development
javascriptcssquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clickok
    wrote on last edited by
    #1

    I wish test if some client side file exists, like swf, jpg, gif, css, etc. Do you know some method to test this? Regards -- modified at 17:36 Wednesday 29th November, 2006 Sample: If one image does not exists, the IE shows a red X icon. I wish with javascript, test if the image does not exists, simply to set css style to hide the image, then this red X icon will not appears. -- modified at 17:36 Wednesday 29th November, 2006


    Jesus is Love! Tell to someone! :badger:

    T 1 Reply Last reply
    0
    • C Clickok

      I wish test if some client side file exists, like swf, jpg, gif, css, etc. Do you know some method to test this? Regards -- modified at 17:36 Wednesday 29th November, 2006 Sample: If one image does not exists, the IE shows a red X icon. I wish with javascript, test if the image does not exists, simply to set css style to hide the image, then this red X icon will not appears. -- modified at 17:36 Wednesday 29th November, 2006


      Jesus is Love! Tell to someone! :badger:

      T Offline
      T Offline
      Torsten Mauz
      wrote on last edited by
      #2

      Well I just hacked this together off the top of my head so it may/may not work properly, but it should get you pointed in the right direction. I would advise you not to go down this road though. In order to get access to the filesystem, the users are required to set their browser's security settings extremely low. This is clearly not a good idea. That's why it's a default setting to have access to this stuff. It's also why you can't do much with file input boxes using javascript. It poses a huge security risk and I repeat I advise you not head down this road. Perhaps if you explained your reasons for wanting to do this we could offer some alternative solutions/ideas.

      var filePath = 'c:\\test.txt';

      if (navigator.userAgent.indexOf('MSIE') == -1 && Components)
      {
      try
      {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

          var file = Components.classes\["@mozilla.org/file/local;1"\].createInstance(Components.interfaces.nsILocalFile);
          file.initWithPath(filePath);
          if (file.exists()) 
          {
              alert('Found!');
          }
          else
          {
              alert('Not found!');
          }
      }
      catch(e)
      {
          alert(e);
      }
      

      }
      else
      {
      try
      {
      var objA = new ActiveXObject('Scripting.FileSystemObject');
      if(objA.FileExists(filePath))
      {
      alert('Found!');
      }
      else
      {
      alert('Not found!');
      }
      }

      catch(e)
      {
          alert(e.message);
      }
      

      }

      The IE stuff will work definately work, but I haven't tested the mozilla code (only have IE installed atm), so not too sure if that will work straight away or not, at the least it should point you in the right direction. HTH

      C 1 Reply Last reply
      0
      • T Torsten Mauz

        Well I just hacked this together off the top of my head so it may/may not work properly, but it should get you pointed in the right direction. I would advise you not to go down this road though. In order to get access to the filesystem, the users are required to set their browser's security settings extremely low. This is clearly not a good idea. That's why it's a default setting to have access to this stuff. It's also why you can't do much with file input boxes using javascript. It poses a huge security risk and I repeat I advise you not head down this road. Perhaps if you explained your reasons for wanting to do this we could offer some alternative solutions/ideas.

        var filePath = 'c:\\test.txt';

        if (navigator.userAgent.indexOf('MSIE') == -1 && Components)
        {
        try
        {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

            var file = Components.classes\["@mozilla.org/file/local;1"\].createInstance(Components.interfaces.nsILocalFile);
            file.initWithPath(filePath);
            if (file.exists()) 
            {
                alert('Found!');
            }
            else
            {
                alert('Not found!');
            }
        }
        catch(e)
        {
            alert(e);
        }
        

        }
        else
        {
        try
        {
        var objA = new ActiveXObject('Scripting.FileSystemObject');
        if(objA.FileExists(filePath))
        {
        alert('Found!');
        }
        else
        {
        alert('Not found!');
        }
        }

        catch(e)
        {
            alert(e.message);
        }
        

        }

        The IE stuff will work definately work, but I haven't tested the mozilla code (only have IE installed atm), so not too sure if that will work straight away or not, at the least it should point you in the right direction. HTH

        C Offline
        C Offline
        Clickok
        wrote on last edited by
        #3

        Thank you Torsten I wish not access none file in user file system, just the files of my web app. By example, my website has one image, located in my site folder "/images/MyImage.jpg". Of course, in server I can use asp.net to check file exists, etc. Resuming: I wish this in javascript. If one image does not exists, the IE shows a red X icon. I wish with javascript, test if the image does not exists, simply set css style to hide the image, then this red X icon will not appears.


        Jesus is Love! Tell to someone! :badger:

        T 1 Reply Last reply
        0
        • C Clickok

          Thank you Torsten I wish not access none file in user file system, just the files of my web app. By example, my website has one image, located in my site folder "/images/MyImage.jpg". Of course, in server I can use asp.net to check file exists, etc. Resuming: I wish this in javascript. If one image does not exists, the IE shows a red X icon. I wish with javascript, test if the image does not exists, simply set css style to hide the image, then this red X icon will not appears.


          Jesus is Love! Tell to someone! :badger:

          T Offline
          T Offline
          Torsten Mauz
          wrote on last edited by
          #4

          Ok when you said "I wish test if some client side file exists" I understood that as that you were checking the client....anyway... this will be a hell of a lot simpler ;)

          Then in a script block below have this:

          var imgEle = document.getElementById('myImg');
          imgEle.onerror = imgError;

          function imgError()
          {
          imgEle.style.display = 'none';
          }

          Note: CP seems to be replacing some of the code...you should replace ".removed" with ".onerror" in the 2nd line of javascript. HTH

          C 1 Reply Last reply
          0
          • T Torsten Mauz

            Ok when you said "I wish test if some client side file exists" I understood that as that you were checking the client....anyway... this will be a hell of a lot simpler ;)

            Then in a script block below have this:

            var imgEle = document.getElementById('myImg');
            imgEle.onerror = imgError;

            function imgError()
            {
            imgEle.style.display = 'none';
            }

            Note: CP seems to be replacing some of the code...you should replace ".removed" with ".onerror" in the 2nd line of javascript. HTH

            C Offline
            C Offline
            Clickok
            wrote on last edited by
            #5

            High level, thank you very much :-) This code works to object tag? like wma, wmv, mp3, swf... Regards


            Jesus is Love! Tell to someone! :badger:

            T 1 Reply Last reply
            0
            • C Clickok

              High level, thank you very much :-) This code works to object tag? like wma, wmv, mp3, swf... Regards


              Jesus is Love! Tell to someone! :badger:

              T Offline
              T Offline
              Torsten Mauz
              wrote on last edited by
              #6

              Off the top of my head, probably not (at least not without some wierd tricks), but feel free to try it and let us know how you get on :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups