16

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

0

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

(Sidenote: I'm not entirely convinced the "Same Origin Policy" isn't playing a role here, since we're also changing the protocol to file:.)
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.
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:/<etc> is included in the sandbox.
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.
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

Comments

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.

5 Comments

if am opening the html as file:///C:/Users/arun.ak/Desktop/somting.html same rule applies?
@ArunKillu: Yes. JavaScript can't access that file from a different domain.
@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
@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 hmm...that i understood later only...i also deleted my answer.but i was not able to convince you.
-1

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

Comments

-5
window.open(url); // here url can be anything

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.