Javascript 打开带有自定义 HTML 而不是 URL 的新选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10573404/
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
Open a new tab with custom HTML instead of a URL
提问by kasper Taeymans
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):
我正在制作一个 Greasemonkey 脚本,并想打开一个新选项卡,该选项卡不会显示 URL,但会显示一些作为脚本一部分的 HTML。所以基本上我想做这样的事情(这显然不起作用):
window.open('<html><head></head><body></body></html>');
or
GM_openInTab('<html><head></head><body></body></html>');
Any hints are welcome!
欢迎任何提示!
回答by aL3891
You can do this:
你可以这样做:
var newWindow = window.open();
var newWindow = window.open();
and then do
然后做
newWindow.document.write("ohai");
newWindow.document.write("ohai");
回答by Noumenon
If the other answer gives you Error: Permission denied to access property "document"
, see this questionabout how to handle same-origin policy problems, or this one.
如果对方的回答给你Error: Permission denied to access property "document"
,看看这个问题如何处理同源策略的问题,或者这一个。
Or, quick and dirty, use a data URI:
或者,快速而肮脏,使用数据 URI:
var html = '<html><head></head><body>ohai</body></html>';
var uri = "data:text/html," + encodeURIComponent(html);
var newWindow = window.open(uri);
回答by Orestis Zekai
Let's say you have a .html
file locally stored. What you can do is this:
假设您在.html
本地存储了一个文件。你可以做的是:
var newWindow = window.open();
newWindow.document.location.href = "/path/to/html/file";