15

Same question as here but I need to go to local URL's in Firefox

I tried with code like

var url = "file:///E:/Test/Test.htm";
window.location.href = url;

but id didn't work. Tried to go with window.location = url; and also tried with url = "file://E:/Test/Test.htm"; (double "/" instead of triple "/") and still doesn't work.

Thanks

Community
  • 1
  • 1
BearCode
  • 2,734
  • 6
  • 34
  • 37

5 Answers5

16

When I try this:

window.location.href = "file:///C:/Users/Cerbrus/Documents/SomeFile.js"

(Yes, it is a valid path.)

Chrome throws me this error:

Not allowed to load local resource: file:///C:/Users//Documents/File.js

This is because JavaScript does not have access to local files (due to it being sandboxed), and you're setting the new url with JavaScript.
"SandBoxed" means a technology has restricted (or no) access outside a certain set of bounds. In the case of browsers, this means that the code that runs on the page can not access files on your system (Otherwise, it would be easy to "steal" data, by just having a look at the user's file system).

However,

Say, I have 2 files:

C:/Test/Test.htm
C:/Test/Test1.htm

Test.htm contains only this:

<script>
    window.location = "file:///C:/Test/Test1.htm";
</script>

This will actually redirect to Test1.htm, since the target file is on the same domain as where the source file's from.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    (Sidenote: I'm not entirely convinced the "[Same Origin Policy](https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript)" isn't playing a role here, since we're also changing the protocol to `file:`.) – Cerbrus Dec 27 '12 at 10:18
  • 2
    Awesome answer! So the script doesn't work from a hosted webpage but it works from a local htm file. But how about Bookmarklets? I need my script in a Bookmarklet. Didn't work for me but Bookmarlkets are local, what is the reason for that? And thanks to the other people who answered too. – BearCode Dec 27 '12 at 13:43
  • That doesn't seem to work, either, @BearBear: `javascript:(function(){window.location="file:///C:/Test/Test1.htm";}());` <-- That gives the same error. I'm guessing the `Test.htm` example I mentioned works, because the source file is local already, so the `file:/` is included in the sandbox. – Cerbrus Dec 27 '12 at 13:48
  • 1
    It's not clear to me how navigating a top-level browsing context to a `file:` URL could give any information to the navigating script, since the navigating script will obviously be terminated. The only case I think where this could potentially be a problem is a `file:` resource in an iframe which might leak some kind of `onload` information, e.g., the existence or size of a file, based on load time. I cannot think of any case in which the navigation of a top-level browsing context could cause information leakage to the navigating script, but I could see it for iframes. – apsillers Dec 15 '15 at 14:00
5

I guess its not allowed to load local resource from javascript

Unless you have a local http server running:

var url = "http://localhost/MySite/Default.aspx";
window.location.href = url;

It will work

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
1

You cannot access the file from the local system. Since the Browser works in the sandbox mode and you cannot breach the sandbox and reach the local file system since it would violate the security. Either try to directly load using an AJAX request else what you are trying to do is not possible due to sandbox restrictions and also does not comply with the security policies.

Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85
  • if am opening the html as file:///C:/Users/arun.ak/Desktop/somting.html same rule applies? – Arun Killu Dec 27 '12 at 09:57
  • @ArunKillu: Yes. JavaScript can't access that file from a different domain. – Cerbrus Dec 27 '12 at 10:12
  • @Cerbrus that i know but when we open the html in which js is written with just a double click (as a file ) then the redirection works – Arun Killu Dec 27 '12 at 12:12
  • @ArunKillu: If that (the html running from `file:`) were the case, OP's code would have worked, and he wouldn't have asked the question. – Cerbrus Dec 27 '12 at 12:14
  • @Cerbrus hmm...that i understood later only...i also deleted my answer.but i was not able to convince you. – Arun Killu Dec 27 '12 at 12:17
-1

window.location.href = window.location.pathname + (your local file name or path)

Bmil
  • 362
  • 2
  • 13
-5
window.open(url); // here url can be anything
arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47