Javascript file existing checking [modified]
-
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:
-
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:
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
-
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
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:
-
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:
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
-
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
-
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:
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 :)