使用 Javascript 转到本地 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14052473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go to local URL with Javascript
提问by BearCode
Same question as herebut I need to go to local URL's in Firefox
与此处相同的问题,但我需要转到 Firefox 中的本地 URL
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.
但 id 没有用。尝试使用window.location = url;并尝试使用url = "file://E:/Test/Test.htm";(双“/”而不是三重“/”),但仍然无法正常工作。
Thanks
谢谢
回答by Cerbrus
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:
Chrome 向我抛出此错误:
Not allowed to load local resource: file:///C:/Users//Documents/File.js
不允许加载本地资源:file:///C:/Users//Documents/File.js
This is because JavaScript does not have access to localfiles (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).
这是因为 JavaScript 无法访问本地文件(因为它被沙箱化了),而您正在使用 JavaScript 设置新的 url。
“SandBoxed”意味着一项技术限制了(或没有)在特定范围之外的访问。对于浏览器,这意味着在页面上运行的代码无法访问您系统上的文件(否则,只需查看用户的文件系统就很容易“窃取”数据)。
However,
然而,
Say, I have 2 files:
说,我有 2 个文件:
C:/Test/Test.htm
C:/Test/Test1.htm
C:/Test/Test.htm
C:/Test/Test1.htm
Test.htmcontains only this:
Test.htm仅包含以下内容:
<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.
这实际上会重定向到Test1.htm,因为目标文件与源文件来自同一域。
回答by Akhil Sekharan
I guess its not allowed to load local resourcefrom javascript
我猜它不允许从 javascript加载本地资源
Unless you have a local http server running:
除非您有本地 http 服务器在运行:
var url = "http://localhost/MySite/Default.aspx";
window.location.href = url;
It will work
它会工作
回答by Shiv Kumar Ganesh
You cannot access the file from the local system. Since the Browser works in the sandbox modeand 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.
您无法从本地系统访问该文件。由于浏览器在沙箱模式下工作,您无法突破沙箱并访问本地文件系统,因为这会违反安全性。要么尝试使用 AJAX 请求直接加载,否则由于沙箱限制,您尝试执行的操作是不可能的,也不符合安全策略。
回答by Bmil
window.location.href = window.location.pathname + (your local file name or path)
window.location.href = window.location.pathname +(你的本地文件名或路径)
回答by arvin_codeHunk
window.open(url); // here url can be anything

