Javascript 在 Chrome 中使用 window.open 打开本地 HTML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5011472/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:14:47  来源:igfitidea点击:

Open a local HTML file using window.open in Chrome

javascript

提问by debugfast

I want to open a local HTML file through Javascript using:

我想使用以下方法通过 Javascript 打开本地 HTML 文件:

window.open ("file://C:/Users/wins/Desktop/exclusiveWordpress.html","mywindow");

But it is opening a new window with a blank page as we used to get when URL is not specified. How do I achieve this?

但是它正在打开一个带有空白页面的新窗口,就像我们在未指定 URL 时所得到的那样。我如何实现这一目标?

回答by DrewT

This worked for me fine:

这对我来说很好:

File 1:

文件 1:

    <html>
    <head></head>
    <body>
        <a href="#" onclick="window.open('file:///D:/Examples/file2.html'); return false">CLICK ME</a>
    </body>
    <footer></footer>
    </html>

File 2:

文件2:

    <html>
        ...
    </html>

This method works regardless of whether or not the 2 files are in the same directory, BUT both files must be local.

无论这两个文件是否在同一目录中,此方法都有效,但两个文件都必须是本地的。

For obvious security reasons, if File 1 is located on a remote server you absolutely cannotopen a file on some client's host computer and trying to do so will open a blank target.

出于明显的安全原因,如果文件 1 位于远程服务器上,您绝对无法在某些客户端的主机上打开文件,并且尝试这样做会打开一个空白目标。

回答by sapana

window.location.href = 'file://///fileserver/upload/Old_Upload/05_06_2019/THRESHOLD/BBH/Look/chrs/Delia';

window.location.href = 'file://///fileserver/upload/Old_Upload/05_06_2019/THRESHOLD/BBH/Look/chrs/Delia';

Nothing Worked for me.

没有什么对我有用。

回答by Nikki Erwin Ramirez

First, make sure that the source page and the target page are both served through the fileURI scheme. You can't force an httppage to open a filepage (but it works the other way around).

首先,确保源页面和目标页面都通过fileURI 方案提供服务。你不能强制一个http页面打开一个file页面(但它反过来工作)。

Next, your script that calls window.open()should be invoked by a user-initiated event, such as clicks, keypresses and the like. Simply calling window.open()won't work.

接下来,您的调用脚本window.open()应该由用户启动的事件调用,例如点击、按键等。简单地打电话是window.open()行不通的。

You can test this right here in this question page. Run these in Chrome's JavaScript console:

您可以在此问题页面中进行测试。在 Chrome 的 JavaScript 控制台中运行这些:

// Does nothing
window.open('http://google.com');

// Click anywhere within this page and the new window opens
$(document.body).unbind('click').click(function() { window.open('http://google.com'); });

// This will open a new window, but it would be blank
$(document.body).unbind('click').click(function() { window.open('file:///path/to/a/local/html/file.html'); });

You can also test if this works with a local file. Here's a sample HTML file that simply loads jQuery:

您还可以测试这是否适用于本地文件。这是一个简单地加载 jQuery 的示例 HTML 文件:

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    </head>
    <body>
        <h5>Feel the presha</h5>
        <h3>Come play my game, I'll test ya</h3>
        <h1>Psycho- somatic- addict- insane!</h1>
    </body>
</html>

Then open Chrome's JavaScript console and run the statements above. The 3rd one will now work.

然后打开 Chrome 的 JavaScript 控制台并运行上面的语句。第三个现在可以工作了。