如何使收藏夹图标出现在新窗口中? [英] How to make the favicon appear in a new window?

查看:58
本文介绍了如何使收藏夹图标出现在新窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开一个新窗口,在该窗口中我将为身体和头部注入HTML.问题出在头部:HTML同时包含标题和图标,但图标不显示.这是代码和jsFiddle: https://jsfiddle.net/ufnjspgc/

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 but the favicon doesn't show. This is the code and the jsFiddle: https://jsfiddle.net/ufnjspgc/

function Start() {

  $('#TheButton').click(function() {

    var TheHeadHTML = '<link href="' + window.location.protocol + '//' + window.location.host + '/favicon.ico" rel="icon" type="image/x-icon">';
    TheHeadHTML = TheHeadHTML + '<title>Title Works</title>';

    var TheNewWindow = window.open();

    $(TheNewWindow.document.head).html(TheHeadHTML);
  });
}

$(Start);

如何使收藏夹图标出现在新窗口中?

How do you make the favicon appear in the new window?

推荐答案

您可以使用

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

<input type="button" value="test" id="TheButton" />

function Start() {

  $('#TheButton').click(function() {
    var TheNewWindow = window.open("data:text/html;charset=utf8,<html><head><link href='" + window.location.protocol + "//" + window.location.host + "/favicon.ico' rel='icon' type='image/x-icon'><title>Title Works</title></head><body></body></html>");
  });
}

$(Start);

小提琴.

基本上,数据URI 允许您在URL本身中指定内容,这样就不需要去服务器,或者就您而言,去"about:blank" 资源.由于跨源和其他方面的考虑,"about:blank"在编写脚本时可能会导致很多问题.

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.

如@ConnorsFan所述,此技术在IE中不起作用.如在此问题中所示. /stackoverflow.com/users/62024/diego-mijelshon">Diego Mijelshon ,

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.

这篇关于如何使收藏夹图标出现在新窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆