Francy Chacko

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromFrancy Chacko
ToMe
SubjectDetecting whether a browser window was opened with ctrl-n
Date27 May 2005 10:34
Hi,
        I checked the previous email with this subject
(http://www.howtocreate.co.uk/emails/BenjaminYogman.html), but on IE (6)
seems it doesn't work.
       
        I tried to open a new window using 3 possible cases, from already
open window.
    a.. Ctrl N --> Here I wasn't able to detect the window.opener
    b.. Through File Menu (same as Ctrl N) --> Here too I wasn't able to
detect the window.opener
    c.. Right click and select "Open Link in New Window" --> Only here I was
able to detect the window.opener
        The code that I used was :

                var win = window.opener;
                if (win!=null) {
                    alert("window.opener.document.title ==
"+window.opener.document.title);
                }

        Can you please guide me how to detect if a window was opened using
the first two cases mentioned above.

Regards,
 francy.
FromMe
ToFrancy Chacko
SubjectRe: Detecting whether a browser window was opened with ctrl-n
Date27 May 2005 11:49
Francy,

>         Can you please guide me how to detect if a window was opened
> using the first two cases mentioned above.

exactly the opposite of what you are doing.

if( !window.opener ) {
    //the window was opened using Ctrl+n, or File - new window, or
    //by opening a new browser instance
} else {
    //the window was openend with script
}

> if (win!=null) {

just a warning, never test against null like this. Some browsers may return
null, but others may return undefined, and these two are not equivalent.
There is no need to text for an explicit value of nothingness anyway, just
use a simple:
if (win) {


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromFrancy Chacko
ToMe
SubjectRe: Detecting whether a browser window was opened with ctrl-n
Date27 May 2005 12:06
Mark,
            Thanks for the prompt reply.

            I followed your suggestions, but the problem is I want to detect
whether the user has refreshed the same page or opened a new instance using
CTRL N. In this case, both of them return true.

Regards,
 francy.
FromMe
ToFrancy Chacko
SubjectRe: Detecting whether a browser window was opened with ctrl-n
Date27 May 2005 21:06
Francy,

>             I followed your suggestions, but the problem is I want to detect
> whether the user has refreshed the same page or opened a new instance using
> CTRL N. In this case, both of them return true.

OK, I see what you mean. Hmm, as far as I can tell, to the browser, these
are essentially equvalent. I was thinking about playing with HTTP headers to
try checking last modified dates, but none of those ideas worked. I also
tried document.referrer, but IE falsifies that on new windows.

OK, I have one last possibility, but it is not perfect;
As the page loads, set a cookie using JavaScript.
As the page unloads, delete the cookie.
Before setting the cookie, check if it already exists. If it does, then the
page was opened with File-New (so in other words, there are two copies of
the page open at the same time). If there is no cookie, then it was a
refresh.

Note that this algorithm will fail;
-if the browser does not fire onunload when refreshing - such as Opera (but
unlike IE, Opera does not normally open the existing page when opening new
windows, so the workaround is probably not necessary anyway)
-if the user rejects cookies

Like I say, it's not perfect, but I am afraid I am out of ideas.

Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.