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 |4 Answers
Reset to default 6This 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.
file:///
instead offile://
– jrn.ak Commented Feb 16, 2011 at 1:10file:
protocol as well? – alex Commented Feb 16, 2011 at 1:11