Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript and Directory Paths

Status
Not open for further replies.

Bones3

Programmer
Jul 27, 2003
151
0
0
US
I am having issues with the Mozilla development platform and am getting tired of XPCOM.

To simplify this mess I want to place the path to the program installation directory in a JavaScript string. I know that JavaScript is not supposed to be able call the OS to get file information for security purposes, right?

If so, what technology/language could I start looking at to put the directory path in a string?

I don't want to have to hardcode this string: it would be nice to make some function like getCurrentDirectory. For example, in C, the #include statement first looks in the file where the executable lives and "knows" that path. That sort of path information is what I want.

-Bones
 
I am not real clear on what you are attempting but...
You CAN browse the local resources using ActiveX which limits you to IE browsers only.
You can use JScript or VBScript to do it within a web page the client accesses but this will prompt the user with an ActiveX warning they have to approve to continue, then the code can read the file information.

What precisely is it you want to accomplish? I can make better recommendations based on the details of the task to be accomplished.

Do you require it to be web based code? You can use a .vbs file to run VBA code or you can use a .hta file that will allow you to use HTML and Javascript/VBScript with client-side privledges but the file has to be executed on the clients PC.


It's hard to think outside the box when I'm trapped in a cubicle.
 
What I'm writing isn't going to be on the web at all: it is an application running on top of the Mozilla platform (like firefox). So anything (including C code) is fair game as long as I can put the string data from C/vb/whatever program into a javascript string without having to store it in a file first.

I guess I basically want to know how to send data from C/vb/whatever into javascript without using files. [because I don't know where the path to the file would be in javascript!] Also, which way is the quickest (e.g. it might take mre time to write it in a low level language like C)?

{If this isn't possible, I'll survive, I did get some ideas from your last post.}

-Bones
 
Bones3, I do not understand what you need to do.

Are you saying you want an executable file that can determine it's own path?
Here is some code that uses Javascript in an .HTA file to retrieve the path.
Getting the path is as simple as using parent.location and this should work in an html file just as easily as a .hta.
The path gets returned as file:///c:/some%20folder/some%20other%20folder/myfile.hta
You obviously will not want the file:///, the %20's should be replaced with real spaces and you probably do not want the executable file name on the end so in the code below I strip those.
Code:
<html>
<head>
<title></title>
   <HTA:APPLICATION
   ID = "oApp"
   APPLICATIONNAME = "My App"
   BORDER = "thick"
   CAPTION = "yes"
   ICON = ""
   SHOWINTASKBAR = "yes"
   SINGLEINSTANCE = "yes"
   SYSMENU = "yes"
   WINDOWSTATE = "normal"
   SCROLL = "yes"
   SCROLLFLAT = "yes"
   VERSION = "1.0"
   INNERBORDER = "yes"
   SELECTION = "no"
   MAXIMIZEBUTTON = "yes"
   MINIMIZEBUTTON = "yes"
   NAVIGABLE = "yes"
   CONTEXTMENU = "yes"
   BORDERSTYLE = "normal"
   >
<SCRIPT type="text/javaScript">
  //Grab current path
  var fullpath = new String(parent.location); //Path including executed filename.

  //Determine the drive letter.
  var drive = fullpath.substring(8,11); // Starts at 9th character and grabs the next three.

  //Set new value stripping out file:/// and the name of the executable file so we have just the path to the folder.
  var eventfolder = drive+fullpath.substring(10,fullpath.lastIndexOf('/')+1); // length- length filename.

  //Replace %20 with a real space.
  var replacetext = /%20/g; 
  eventfolder = eventfolder.replace(replacetext," ");

  alert(eventfolder);
</script>
</head>
<body>
</body>
</html>

Save this file to your desktop with a .hta extension and it will be executable just by clicking on it.

As I said though, this obtains the path using parent.location which is just normal Javascript. You can strip out the HTA tag and just run it as html and it works there as well as long as you are running it local to the PC and not from a web page. Otherwise the path returned would be that of the server not the local PC.

If you need to do any file manipulation like creating files/folders, etc, then you could leave this as a .hta application and you have access to the local file system.


Google, you're my hero!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top