简体   繁体   English

在磁盘上的新浏览器选项卡中使用 javascript 以编程方式打开 html 文件

[英]Open an html file programatically using javascript in a new browser tab from disk

Is there a way to open an html file using javascript in a new tab?有没有办法在新选项卡中使用 javascript 打开 html 文件?

I would want the user to select an html file using the <input type="file"> .我希望用户使用<input type="file">选择一个 html 文件。 After selecting a file, some JavaScript code will open that html file.选择文件后,一些 JavaScript 代码将打开该 html 文件。

You can use the File API for that.您可以为此使用文件 API。 You could do something like the following example.您可以执行类似于以下示例的操作。

Here is a live example这是一个活生生的例子

Tested it only in the latest Chrome & Firefox versions.仅在最新的 Chrome 和 Firefox 版本中对其进行了测试。

HTML: HTML:

<form action="">
    <p><input type="file" id="userFile"></p>
</form>

<p><a href="" target="_blank" id="newTab" style="display:none">Open File</a></p>
<div id="preview"></div>

**JS:** **JS:**
 (function(window, undefined) { var doc = window.document, userFile = doc.getElementById('userFile'), newTab = doc.getElementById('newTab'), preview = doc.getElementById('preview'), fileReader = new FileReader(); var fileutil = { init: function() { userFile.addEventListener('change', fileutil.onchange, false); }, onchange: function(e) { //console.log(this.files); fileutil.create(this.files[0]); }, create: function(file) { //console.log(file); var iframe = doc.createElement('iframe'); // Start reading file..., and wait to complete. fileReader.readAsDataURL(file); // When done reading. fileReader.onload = function(e) { //console.log(e.target.result); iframe.src = e.target.result; iframe.width = '100%'; iframe.height = 500; newTab.href = e.target.result; newTab.style.display = 'block'; preview.appendChild(iframe); }; } }; fileutil.init(); }(this));

UPDATE: ------- This example gives access to the iframe's document, therefore allowing you to communicate with it and change its contents. 更新: ------- 此示例提供对 iframe 文档的访问权限,因此允许您与其进行通信并更改其内容。

JSFiddle: http://jsfiddle.net/Lxx56hh4/ JSFiddle: http : //jsfiddle.net/Lxx56hh4/

JS: JS:

 (function(window, undefined) { var doc = window.document, userFile = doc.getElementById('userFile'), preview = doc.getElementById('preview'), // We read the file as Text and then parse it into a DOM Document. fileReader = new FileReader(), domParser = new DOMParser(); var fileutil = { init: function() { userFile.addEventListener('change', fileutil.onchange, false); }, onchange: function(e) { fileutil.create(this.files[0]); }, create: function(file) { var self = this; // An iframe to append the new Document to. this.iframe = doc.createElement('iframe'); this.iframe.width = '100%'; this.iframe.height = 300; // Start reading the file as plain text and wait to complete. fileReader.readAsText(file); // When done reading. fileReader.onload = function(e) { if (e.target.readyState === 2) { // 2 means DONE preview.appendChild(self.iframe); fileutil._ready(e.target.result); } }; }, _ready: function(ihtml) { var iwindow = this.iframe.contentWindow; var idocument = iwindow.contentDocument || iwindow.document; // Create a DOM document out of the text contents. var fileDocument = domParser.parseFromString(ihtml, "text/html"); // Make the iframe's body equal the fileDocument's body. // We do this so we get only the body's contents and not the whole document. idocument.body.innerHTML = fileDocument.body.innerHTML; // We can now communicate with the iframe's body to add or remove elements. // With jQuery you can do: // `$(idocument.body).prepend('<h1>Injected H1.</h1>')` // // NOTE: Any resources such as stylesheets, images, and scripts referenced in fileDocument may not be available in the iframe. // // With VanillaJS you can do: idocument.body.insertAdjacentHTML('afterbegin', '<h1>Injected H1.</h1>'); // Using XHR2 you can now send the original file or DOMString to your server. // More here: http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-sending } }; fileutil.init(); }(this));

HTML: HTML:

 <form action=""> <p><input type="file" id="userFile"></p> </form> <div id="preview"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript在Chrome浏览器的新标签页中以编程方式打开URL - Programatically open the URL in new tab in chromium browser using javascript 使用JavaScript从扩展程序中以编程方式打开Firefox浏览器控制台? - Open the Firefox browser console programatically from within an extension using JavaScript? 使用Javascript从Drupal打开新标签页 - Open New Tab From Drupal Using Javascript 使用 Javascript 在新的浏览器选项卡中使用自定义磁贴打开 PDF blob 文件 - Open PDF blob file in new browser tab with custom tile using Javascript 如何使用 javascript 在浏览器的新选项卡中打开本地文件路径? - How to open a local file path in new tab in my browser using javascript? 使用JavaScript在Google Chrome浏览器的打开新标签页窗口中出现的问题 - Issue in open new tab window for Google Chrome Browser using javascript 如何打开新的浏览器窗口(不是选项卡),然后使用jQuery / Javascript调整其大小 - How to open new browser window (not tab) and then resize it using jQuery / Javascript 如何在JavaScript中打开新的浏览器标签? - How can I open a new browser tab from within JavaScript? 在新标签浏览器中打开响应 html - open response html in new tab browser JavaScript在新标签页中打开,而不是Chrome浏览器中的窗口 - JavaScript open in a new tab, not window in Chrome browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM