views:

963

answers:

4

How can I tell in JavaScript what path separator is used in the OS where the script is runnning?

A: 

Why do you need this when Javascript is run inside the browser with complete OS isolation? You can't access files from your local computer...

lms
JavaScript is usually, but not always, run in the browser. Some examples of JavaScript uses outside the browser are Mozilla XULRunner apps, js scripts run using a command-line interpreter like V8 or WSH, and ASP pages written in JScript. There are many uses for JS.
Joel Anair
A: 

Just use "/", it works on all OS's as far as I know.

Vincent McNabb
+5  A: 

Afair you can always use / as a path separator, even on Windows.

Quote from http://bytes.com/forum/thread23123.html:

So, the situation can be summed up rather simply:

  • All DOS services since DOS 2.0 and all Windows APIs accept either forward slash or backslash. Always have.

  • None of the standard command shells (CMD or COMMAND) will accept forward slashes. Even the "cd ./tmp" example given in a previous post fails.

VVS
A: 

VVS's answer is correct, with the exception of parsing a path given by a file input in internet explorer (tested with IE8 - I do not know about other versions). In this case, the path given by the input element's value (input.value) is in the form "C:\fakepath\<filename>". Note the backslashes here.

Milagre