使用 Javascript 转到本地 URL [英] Go to local URL with Javascript

查看:40
本文介绍了使用 Javascript 转到本地 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里相同的问题,但我需要去本地Firefox 中的 URL

我尝试过类似的代码

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

但 id 不起作用.尝试使用 window.location = url; 并尝试使用 url = "file://E:/​​Test/Test.htm"; (double "/"而不是三个/"),但仍然不起作用.

谢谢

解决方案

当我尝试这个时:

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

(是的,这是一个有效的路径.)

Chrome 向我抛出此错误:

<块引用>

不允许加载本地资源:file:///C:/Users//Documents/File.js

这是因为 JavaScript 无权访问本地文件(因为它被沙盒化了),而您正在使用 JavaScript 设置新网址.
SandBoxed"意味着一项技术限制了(或没有)在特定范围之外的访问.就浏览器而言,这意味着在页面上运行的代码无法访问您系统上的文件(否则,只需查看用户的文件系统,就很容易窃取"数据).

不过

比如说,我有 2 个文件:

<块引用>

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

Test.htm 仅包含以下内容:

这实际上会重定向到 Test1.htm,因为目标文件与源文件来自同一域.

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

解决方案

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.

这篇关于使用 Javascript 转到本地 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆