Open a new tab with custom HTML instead of a URL

后端 未结 3 497
情书的邮戳
情书的邮戳 2021-02-02 06:17

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 lik

相关标签:
3条回答
  • 2021-02-02 06:44

    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";
    
    0 讨论(0)
  • 2021-02-02 06:56

    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);
    
    0 讨论(0)
  • 2021-02-02 07:03

    You can do this:

    var newWindow = window.open();

    and then do

    newWindow.document.write("ohai");

    0 讨论(0)
提交回复
热议问题