How to make the favicon appear in a new window?

后端 未结 7 1599
无人共我
无人共我 2021-01-17 22:17

I\'m opening a new window into which I\'m injecting HTML for both the body and the head. The problem is in the head section: the HTML includes both the title and the favicon

7条回答
  •  没有蜡笔的小新
    2021-01-17 23:09

    You can open a new window using a data URI. Here's the code:

    
    
    function Start() {
    
      $('#TheButton').click(function() {
        var TheNewWindow = window.open("data:text/html;charset=utf8,Title Works");
      });
    }
    
    $(Start);
    

    And the fiddle.

    Basically, data URIs allow you to specify the content in the URL itself such that it doesn't need to go to a server, or, in your case, to the "about:blank" resource browsers (must) have. "about:blank" can cause a lot of problems when scripting because of cross-origin and other concerns.

    As noted by @ConnorsFan, this technique does not work in IE. As indicated in this question by Diego Mijelshon, IE does not allow navigation to a data URI, and thus it cannot be used as the URL for a new window. Seems to work fine in recent versions of Chrome and Firefox. I'm afraid I don't have a copy of Safari on which to test.

提交回复
热议问题