简体   繁体   中英

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.

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 .

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:

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.

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. So, 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 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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