Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2012, 04:32 PM   PM User | #1
tdavis
Regular Coder

 
Join Date: Feb 2005
Location: Atlanta, GA
Posts: 171
Thanks: 5
Thanked 0 Times in 0 Posts
tdavis is an unknown quantity at this point
Check for file existence

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
%>
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0042)http://intapps.corp.cox.com/apps/icomsgui/ -->
<HTML><HEAD><TITLE>ICOMS GUI Home Page</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type><LINK
rel=stylesheet type=text/css href="icomsgui_files/styles.css">
<SCRIPT language=JavaScript type=text/JavaScript>
<!--
<!-- Opens windows -->

function open_win1()
{
window.open("http://data1gui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}

function open_win2()
{
window.open("http://data2gui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}

function open_win3()
{
window.open("http://centralgui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}



//-->
</SCRIPT>

<META name=GENERATOR content="MSHTML 8.00.6001.19258"></HEAD>
<BODY bgColor=#ffffff>
<TABLE border=0 cellSpacing=0 cellPadding=0 width=786 align=center>
  <TBODY>
  <TR vAlign=bottom>
    <TD height=36 colSpan=3><IMG src="icomsgui_files/header.jpg" width=786
      height=35></TD></TR>
  <TR>
    <TD vAlign=top width=13><IMG src="icomsgui_files/borderlft.jpg" width=10
      height=388></TD>
    <TD vAlign=top width=760>
      <TABLE border=0 cellSpacing=0 cellPadding=0 width=760>
        <TBODY>
        <TR>
          <TD vAlign=top width=399><IMG src="icomsgui_files/coxlogo.jpg"
            width=399 height=94></TD>
          <TD vAlign=top width=361><IMG src="icomsgui_files/header2.jpg"
            width=361 height=22></TD></TR>
        <TR vAlign=top>
          <TD colSpan=2>
            <DIV align=center><IMG src="icomsgui_files/icomslogo.jpg" width=431
            height=141></DIV></TD></TR>
        <TR>
          <TD colSpan=2>
            <DIV align=center>
            <P class=txtStandard><BR>Please make a selection from the list
            below</P><!-- form table -->
            <TABLE border=0 cellSpacing=0 cellPadding=0 width=400>
              <TBODY>
              <TR>
                <TD vAlign=top>
                  <FORM method=post name=form1 action=icomsgui.html>
                  <TABLE width=400>
                    <TBODY>
                    <TR class=txtStandard>

                      <TD><INPUT onclick=open_win1() value=Data1 type=radio
                        name=databases> <A
                        href="javascript:open_win1();">Data1</A> </TD>

                      <TD><LABEL><INPUT onclick=open_win2() value=Data2
                        type=radio name=databases> <A
                        href="javascript:open_win2();">Data2</A></LABEL></TD>

                      <TD><LABEL><INPUT onclick=open_win3() value=Central
                        type=radio name=databases> <A
                        href="javascript:open_win3();">Central</A></LABEL></TD>

                      </TR>

                        </TBODY></TABLE></FORM></TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></TD>
    <TD vAlign=top width=16><IMG src="icomsgui_files/borderrgt.jpg" width=16
      height=388></TD></TR>
  <TR vAlign=top>
    <TD height=17 colSpan=3><IMG src="icomsgui_files/borderbtm.jpg" width=786
      height=17></TD></TR></TBODY></TABLE>
<P align=center>&nbsp;</P></BODY></HTML>
tdavis is offline   Reply With Quote
Old 06-30-2012, 05:00 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by tdavis View Post
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.

Last edited by Philip M; 06-30-2012 at 05:05 PM..
Philip M is offline   Reply With Quote
Old 06-30-2012, 05:22 PM   PM User | #3
tdavis
Regular Coder

 
Join Date: Feb 2005
Location: Atlanta, GA
Posts: 171
Thanks: 5
Thanked 0 Times in 0 Posts
tdavis is an unknown quantity at this point
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?
tdavis is offline   Reply With Quote
Old 06-30-2012, 07:26 PM   PM User | #4
tdavis
Regular Coder

 
Join Date: Feb 2005
Location: Atlanta, GA
Posts: 171
Thanks: 5
Thanked 0 Times in 0 Posts
tdavis is an unknown quantity at this point
what about this?

Code:
<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>
tdavis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:55 PM.


Advertisement
Log in to turn off these ads.