简体   繁体   English

有没有办法跟踪使用Javascript从哪个window.name打开新窗口(在新选项卡中打开)?

[英]Is there a way to track from which window.name a new window was opened (open in a new tab) with Javascript?

I need to track all users browser activity from a starting point. 我需要从起点跟踪所有用户的浏览器活动。 All starting points have unique window.name assigned to them. 所有起点均分配有唯一的window.name

If a user clicks open in a new tab, I will lose this unique identifier to actually track each starting point. 如果用户单击新选项卡中的打开,我将失去此唯一标识符以实际跟踪每个起点。 I need to find a somewhere an association between these two windows (the child and the parent window from which the child was triggered). 我需要在某个地方找到这两个窗口(子级和从中触发子级的父级窗口)之间的关联。

Can the "open in a new tab" be handled as an event so I can attach an event listener? 是否可以将“在新标签页中打开”作为事件处理,以便可以附加事件侦听器? Or what other solutions are there? 或者还有什么其他解决方案?

You've clarified your question in the comments below. 您已在下面的评论中澄清了您的问题。 You're asking specifically about the user right-clicking a link and choosing "Open in new tab." 您要特别询问用户右键单击链接并选择“在新选项卡中打开”。 (As opposed to the user clicking links that you've set up to open in new tabs, which is how I read it originally.) (与用户单击设置为在新标签页中打开的链接相反,这是我最初阅读的方式。)

The simplest and most reliable way to do that is to modify the links on the page to pass the window name as a query string parameter: 最简单,最可靠的方法是修改页面上的链接,以将窗口名称作为查询字符串参数传递:

var i, list, href;
list = document.getElementsByTagName("a");
for (i = 0; i < list.length; ++i) {
    href = list[i].href;
    if (href.indexOf("?") === -1) {
        href += "?wnd=" + encodeURIComponent(window.name);
    }
    else {
        href += "&wnd=" + encodeURIComponent(window.name);
    }
}

Then the target page just checks for the query string parameter in location.search . 然后目标页面仅检查location.search的查询字符串参数。

Alternately, you can set some information in web session storage when the user clicks the link, and then check for it on the target page: 或者,您可以在用户单击链接时在Web会话存储中设置一些信息,然后在目标页面上进行检查:

On the first page: 在第一页上:

var i, list;
list = document.getElementsByTagName("a");
for (i = 0; i < list.length; ++i) {
    list[i].addEventListener("click", setWindowName, false);
    // If you need to handle IE8, you'll need to do the addEventListener / attachEvent dance
}
function setWindowName() {
    sessionStorage.wndname = window.name;
}

Then on the target page: 然后在目标页面上:

var sourceWindow = sessionStorage.wndname;

Other than that, you can give them a link that will open the window in a new tab for them (see below), but if they right-click the link and say "open in new tab," I don't think there's any connection between the two windows. 除此之外,您可以给他们提供一个链接,该链接将为他们打开一个新标签页中的窗口(请参见下文),但是如果他们右键单击该链接并说“在新标签页中打开”,我认为这没有任何意义两个窗口之间的连接。 On the server side you could check the REFERER [sic] HTTP header, but I don't think there's anything purely client-side. 服务器端,您可以检查REFERER [sic] HTTP标头,但我认为没有任何纯粹的客户端。

Can i handle "open in a new tab" as an event... 我可以将“在新标签页中打开”作为事件进行处理吗?

I don't believe any event is raised when the user opens a link in a new window or tab. 我认为用户在新窗口或新标签页中打开链接时不会引发任何事件。


If the window is opened via window.open or a link with target="..." , the new window will have an opener global, which is a reference to the window that opened it. 如果通过window.open或带有target="..."的链接打开窗口,则新窗口将具有一个全局的opener ,该opener是对打开该窗口的引用。 So, opener.name . 因此, opener.name

Parent window example: Live Copy | 父窗口示例: 实时复制 | Live Source 现场直播

window.name = "parentwin";
document.querySelector('p').onclick = function() {
    window.open("/uTEzowOQ/1");
};

Or alternately via a link: Live Copy | 或通过链接: Live Copy | Live Source 现场直播

<p><a href="/uTEzowOQ/1" target="_blank">Click me</a></p>
<script>
window.name = "parentwin";
</script>

Child window: Source 子窗口: 来源

if (!opener) {
    display("My <code>opener</code> isn't set.");
}
else if (!opener.name) {
    display("<code>opener.name</code> is blank");
}
else {
    display("My opener's name is " + opener.name);
}

Note that if you just go directly to the child page , it says "My opener isn't set". 请注意,如果您直接进入子页面 ,则会显示“未设置我的开启器”。 But if you go there via either of the parent pages above, you see "My opener's name is parentwin". 但是,如果您通过上面的任一父页面转到那里,则会看到“我的揭幕战的名字是parentwin”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM