From: Csaba Gabor on
How can I most reliably detect when a link has been clicked to open in
a new tab or to open in a new window? I have not found this question
well adressed in the pages returned up my searches.

I am doing some statistics gathering. In particular, I have some
links that point to external websites (they are product links) and I
would like to know when they are clicked, to verify against what the
destination site reports. If they are clicked "normally", to open in
the same window, it is no problem because one just adds an onclick
handler to the link. All my onclick handler does is to send a request
out via an embedded Iframe (so it can be logged by the server), and
this request gets sent before the new page starts to load.

However, the onclick does not fire when the link is opened in a new
window or new tab (and that's how I always click my links because I
don't want to lose the previous page). It is insufficient to detect
oncontextmenu because the user might not activate the link (and I'd
rather not have false positives). I don't need a bulletproof solution
- I will be quite happy if the detection mechanisms works a good
percentage of the time in recent versions of IE and FF.

Regards,
Csaba Gabor from Vienna
From: Evertjan. on
Csaba Gabor wrote on 21 jun 2010 in comp.lang.javascript:

> How can I most reliably detect when a link has been clicked to open in
> a new tab or to open in a new window?

Only by looking at the window.

Tabs are browser specific settings that are outside the reach of the DOM.

===============

Perhaps you could measure the old and new page witdhs and compare those
with the specified wondow.open() width?

If the requested width is 20px, and the old and new witdh are both the same
value and >250px, I would guess it is a tab.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: SAM on
Le 6/21/10 11:13 AM, Csaba Gabor a �crit :
> How can I most reliably detect when a link has been clicked to open in
> a new tab or to open in a new window? I have not found this question
> well adressed in the pages returned up my searches.
>
> I am doing some statistics gathering. In particular, I have some
> links that point to external websites (they are product links) and I
> would like to know when they are clicked, to verify against what the
> destination site reports. If they are clicked "normally", to open in
> the same window, it is no problem because one just adds an onclick
> handler to the link.

So, your counter works only if JS is enabled ? !

> All my onclick handler does is to send a request
> out via an embedded Iframe (so it can be logged by the server), and
> this request gets sent before the new page starts to load.

If is only a question of clicks count,
instead of onclick maybe onmousedown, onmouseup ?
Or detect the no left-click on mouse down ?

<a href="http://google.com"
onmousedown="var e = event.wich||event.button;
if(e>1) parent.myInvisibleIframe.location='myCountr.php?c='+this.href;"
onclick="window.open('myCountr.php?c='+this.href,'view','width=300,height=300');
return false;">test with google</a>

<iframe name="myInvisibleIframe"
style="visibility:hidden;width:1px"></iframe>

(not tested with IE)
--
sm
From: Thomas 'PointedEars' Lahn on
Csaba Gabor wrote:

> How can I most reliably detect when a link has been clicked to open in
> a new tab or to open in a new window? I have not found this question
> well adressed in the pages returned up my searches.

You have not found anything because you can't do this nor should you try.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
From: Csaba Gabor on
On Jun 21, 11:50 am, "Evertjan." <exjxw.hannivo...(a)interxnl.net>
wrote:
> Csaba  Gabor wrote on 21 jun 2010 in comp.lang.javascript:
>
> > How can I most reliably detect when a link has been clicked to open in
> > a new tab or to open in a new window?
>
> Only by looking at the window.

Hi Evertjan,
these links lead to external pages so that I don't have access to
the DOM. The only thing that I can see is possible effects on the
original page in which the link has been clicked.

> Tabs are browser specific settings that are outside the reach of the DOM.
>
> ===============
>
> Perhaps you could measure the old and new page witdhs and compare those
> with the specified wondow.open() width?
>
> If the requested width is 20px, and the old and new witdh are both the same
> value and >250px, I would guess it is a tab.

I like the brainstorming, but I don't think this is relevant here as
there is no window.open() involved. Ie. a right click, open in new
tab/window will open what is in href=..." in the relevant window, but
not use a javascript window.open() to do so.

> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)