Can javascript check for the existence of a file or folder on the local hard drive? I need to modify the following script so that I have six locations rather than three; that is, if a file exists, use these three, else use these three (per the window.open functions in the code below). I found that ASP can do this, but I am not a regular coder, so I am not sure. Any help is appreciated!
Code:
<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("c:\asp\introduction.asp")=true then
response.write("File c:\asp\introduction.asp exists!")
else
response.write("File c:\asp\introduction.asp does not exist!")
end if
set fs=nothing
%>
Can javascript check for the existence of a file or folder on the local hard drive?
Simple asnwer - (generally) no.
JavaScript running in a browser is a client-side language. JavaScript does not have any commands for reading or writing files. Modern browsers can read files on the server using an Ajax call, but otherwise JavaScript has no capability to read from, write to, modify or delete a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser.
Reading from an arbitrary location would be a huge security hole.
It is possible using FileSystemObject (if you use Windows Scripting Host or Internet Explorer in a trusted environment). That makes it suitable only for intranet applications.
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
This is an intranet page. one that directs users to this server or that server. The issue is one version of the software versus another. we want to install in groups rather than everyone at once. But since javascript cannot do it, which is what I thought, I will have to explore other ideas. I do not know what windows scripting is. what about PHP? Can I incorporate the ASP code into the page and use that?
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// if condition to check whether the specified file exists or not.
if (fso.FileExists("C:\\Temp\\myFolder\\file.txt")) {
document.write("File.txt exists.");
}
else {
document.write("File.txt does not exist.");
}
fso = null;
</script>