Open a new tab with custom HTML instead of a URL

我只是一个虾纸丫 提交于 2019-12-03 08:16:42

问题


I'm making a Greasemonkey script and would like to open a new tab which will not display a URL but some HTML that is part of the script. So basically I want to do something like this (which is obviously not working):

window.open('<html><head></head><body></body></html>');
or
GM_openInTab('<html><head></head><body></body></html>');

Any hints are welcome!


回答1:


You can do this:

var newWindow = window.open();

and then do

newWindow.document.write("ohai");




回答2:


If the other answer gives you Error: Permission denied to access property "document", see this question about how to handle same-origin policy problems, or this one.

Or, quick and dirty, use a data URI:

var html = '<html><head></head><body>ohai</body></html>';
var uri = "data:text/html," + encodeURIComponent(html);
var newWindow = window.open(uri);



回答3:


Let's say you have a .html file locally stored. What you can do is this:

var newWindow = window.open();
newWindow.document.location.href = "/path/to/html/file";


来源:https://stackoverflow.com/questions/10573404/open-a-new-tab-with-custom-html-instead-of-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!