最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open a local HTML file using window.open in Chrome - Stack Overflow

programmeradmin1浏览0评论

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

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?

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

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?

Share Improve this question edited Apr 12, 2018 at 12:05 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges259 bronze badges asked Feb 16, 2011 at 1:09 ViswaViswa 1,3773 gold badges18 silver badges30 bronze badges 4
  • 6 try file:/// instead of file:// – jrn.ak Commented Feb 16, 2011 at 1:10
  • Is the page with this HTML on file: protocol as well? – alex Commented Feb 16, 2011 at 1:11
  • 1 ya its a html file. I have tries with file:/// instead of file://.........still its not working as expected – Viswa Commented Feb 16, 2011 at 1:15
  • 1 If the original web page is coming from http{s}, you can't (security issues). Could you specify where the javascript is being run from (localhost, http://, https://, file://, ... telling us that its an html file is not specific enough) – technosaurus Commented Jul 26, 2015 at 1:23
Add a comment  | 

4 Answers 4

Reset to default 6

This worked for me fine:

File 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:

    <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 cannot open a file on some client's host computer and trying to do so will open a blank target.

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

Nothing Worked for me.

Here's a solution that worked for my use case. I needed to open a local HTML page in a new tab like so:

window.open("http://localhost:5173/newPage/newPage.html", "_blank");

but of course, this wouldn't work once I deployed to production server. My solution was to grab the domain name using window.location.hostname and then insert that into the URL:

let domainName = window.location.hostname;

window.open(`http://${domainName}:5173/page/page.html`, "_blank");

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

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.

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

// 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:

<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.

发布评论

评论列表(0)

  1. 暂无评论