Detecting open in new tab / window

C

Csaba Gabor

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
 
E

Evertjan.

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.
 
S

SAM

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)
 
T

Thomas 'PointedEars' Lahn

Csaba said:
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
 
C

Csaba Gabor

Csaba  Gabor wrote on 21 jun 2010 in comp.lang.javascript:


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.
 
C

Csaba Gabor

Le 6/21/10 11:13 AM, Csaba Gabor a crit :



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

Correct. There is no pretense here that I am
collecting all possible link activations. In fact,
I have one snippet of code (for the main page) to
detect whether JS is enabled, and another that
checks for cookies (this is to try to gain an
understanding of how people are coming to the
site), and another to check whether the page is
a chached copy. None of these effects the
minimal functionality of the site.

Oh, and the site is pretty basic - it's just a
short list of (ballpark 60) items (books) with
reviews, separated onto 3 distinct pages.
Nonetheless, google indexes it highly so I do
get visitors, though it's a fairly fresh site.
If is only a question of clicks count,

I am specifically after all actual product link
'activations' that I can be certain of without
introducing any false positives. Therefore,
click counts are not sufficient.
instead of onclick maybe onmousedown, onmouseup ?
Or detect the no left-click on mouse down ?

Thanks for this sample code. I am doing on
onclick what you are doing in onmousedown.
That gives me actual link openings in the current
window. However, as I read this code, it doesn't
cover me for right clicks.
 
G

Gregor Kofler

Am 2010-06-21 11:13, Csaba Gabor meinte:
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.

Because you can't.

Gregor
 
C

Csaba Gabor

You have not found anything because you can't do this

Your argument, or lack thereof, is not convincing.
By saying that I can't do this, you are asserting
that there is no detectable change in/on the page
that has had such a link clicked. Is that really
true?
nor should you try.

Your pronouncment on my value system does not
belong in this forum. "I would not try,"
would be much more believable, though perhaps
not very relevant.
 
A

Asen Bozhilov

Csaba said:
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.

One possible approach is server side proxy. If you detect click on
link log statistic and request external page in iframe, otherwise
request your page on same server. There will be server side script
which log statistic and relocate to external page. In my opinion that
is simple solution for your problem.
 
T

Thomas 'PointedEars' Lahn

Csaba said:
Your argument, or lack thereof, is not convincing.
By saying that I can't do this, you are asserting
that there is no detectable change in/on the page
that has had such a link clicked.

No, I am asserting that there is no reliable way to detect this, since that
was your condition.
Is that really true?

Your logic is flawed.
Your pronouncment on my value system does not belong in this forum.
"I would not try," would be much more believable, though perhaps
not very relevant.

Reading luser responses such as this makes me wonder why I still bother
investing my free time for an answer, offering my knowledge and experience
for free.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Asen said:
One possible approach is server side proxy. If you detect click on
link log statistic and request external page in iframe, otherwise
request your page on same server. There will be server side script
which log statistic and relocate to external page. In my opinion that
is simple solution for your problem.

But HTTP proxying will not serve to detect if a new viewport was created to
view the referred resource. Nor can anything else, reliably.


PointedEars
 
A

Asen Bozhilov

Thomas said:
Asen Bozhilov wrote:

But HTTP proxying will not serve to detect if a new viewport was created to
view the referred resource.  Nor can anything else, reliably.

Of course, but is that really need? He just not able to detect it.
With suggested approach, if that resource is requested that mean a
resource has not been requested from him page after regular click on
link. I think he wants that. If he wants to detect opened new windows
there is something wrong. For example in Firefox extensions that is
possible by XPConnect but in browser scripting that is not possible,
especially when we talk about cross domain policy.
 
E

Evertjan.

Csaba Gabor wrote on 21 jun 2010 in comp.lang.javascript:
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.

Use Chrome and on right-click you can choose.

But that is non of the buziness of the javascript writer to know or
manipulate on.
 
D

Dr_Kral

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.
As explained by several posters already doing this client side is certainly
impossible on the face without JS allowed and you may not count on JS
being active (which would not help).

The answer is therefore you must do it server side.

If you can trust the clients, then making the link trigger a counter in
their server will do the job. You say you can do this.

Then you must do it in your own server. All the links point to your server
and then you redirect to the client's server. Or you can mirror the
client's pages in your server in some case for faster response.

K.
 
C

Csaba Gabor

I agree. That would be the solution I'd prefer if I had to log all link
clicks, even if the eventual target is an external page. I think that
was the OP's main point, instead of recognizing when a new tab (or
window) was opened.

Correct. I have no particular interest in how a user
gets to an external page. I am only interested in
knowing whether s/he went to the external page. The
motivations here being (1) to double check that what
the remote sites report back is not undercounted.
(2) to double check how well my monitoring reflects
reality.
If that was really important, it might be possible to use history.length
in some cases:

I didn't mention this explicitly in the original
post, but these product links generally have the
proviso that you may not alter the link, and they
sometimes have robots visit the site to verify
this. I think that's reasonable. The external
product sites certainly have a vested interest in a
standard appearance. Deviation from a standard
appearance/behaviour may lead to confusion and
suspicion on the part of prospective customers.

Furthermore, these product links tend to have a
prohibition against the kind of redirection that
I believe you (and Asen) are implying. But even
if were to trap for their robots and only put in
this redirection when it's not a robot (a very
big if), I still wouldn't want to do this because
the users could see in the status bar that the
link href wasn't directing them to where the link
text was implying they should go. In other words,
I don't want to change the user experience
whatsoever.

And by the way, wouldn't the remote sites detect
the redirection by noting the referer?
 - let all external links point to a script on your site, for example
   /tracker?t=http%3A%2F%2Fwww.example.com%2F

 - if JavaScript is enabled, add an additional parameter to this link:
   /tracker?t=http%3A%2F%2Fwww.example.com%2F&js=1

 - in the 'tracker' script, log the link access and
   a) without the 'js' parameter, redirect to the link target
   b) with js=1, send back a small HTML document which reads
      history.length, sends the result back to the server, and uses
      history.replace() to direct the visitor to the target site. A
      length of 1 would suggest that the link has been opened in a new
      tab/window. You could use other indicators, like the 'Referer'
      header, cookies, or an additional link parameter to distinguish
      this case from links which were opened from bookmarks, or directly
      entered in the address bar.

I don't recommend it. It's not going to work in all cases, and some
visitors may object (especially if you name the server side script

We are in agreement here.
'tracker' ;-), but that's the only thing that comes to my mind if you
really really need to detect opened tabs.

I have now solved the problem I asked about to my
satisfaction (verified on IE 6, IE 8, FF 1.5) - I
am able to detect navigation to external sites in
JS enabled browsers in most cases that will matter
to me without changing the user experience. I can
differentiate between navigation within the same
window (ie. normal clicks) vs. opening the external
link in either a new tab or new window (but I can't
differentiate between a new tab vs. new window).
The code is compact, and seems to be fairly standard
for this kind of thing. I just hadn't searched under
the right terms initially.

Csaba
 
C

Csaba Gabor

Wouldn't it have been better to write, "You can't
reliably do what you want on account of xyz", and
then I would have realized that you had
misunderstood my intent and could have adressed
the technical issues rather than making a detour.

The thing is I know that you know how to do this
because I've seen at least one post from addressing
this topic. And the reason that I didn't find
anything on it was because the main work on this
topic is categorized differently than how I was
approaching it.
No, I am asserting that there is no reliable way to detect this, since that
was your condition.

Incorrect. My problem statement did not use the word
reliable. You have used this word first. I said
most reliably which is substantially different.
And lest it be unclear, I clarified in the third
paragraph of my initial post:
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.

I want something does not report false positives
and that is better than what I had (which was
nothing).
Your logic is flawed.



Reading luser responses such as this makes me wonder why I still bother
investing my free time for an answer, offering my knowledge and experience
for free.

Hurrah! A bright point in the thread. I have
been wondering the same thing about you, too.
The thing is, you actually have helpful responses
sometimes, and I do appreciate those, but so
often what filters through to me is the vitriol,
dogmatic pronouncements, and especially the
moralizing and assertions about a person's inner
state of mind. So I am supportive of an action
on your part to reduce this type of post in the
future.
 
T

Thomas 'PointedEars' Lahn

Csaba said:
Wouldn't it have been better to write, "You can't
reliably do what you want on account of xyz",

No. That there would be a "most reliable" way implies that there is a
reliable one to begin with. There is not. And if you had paid attention
you would have seen that I am not the only one having that issue with your
question.
No, I am asserting that there is no reliable way to detect this, since
that was your condition.

Incorrect. My problem statement did not use the word
reliable. You have used this word first [...]

That is true, you have only used its adverb, and now you go to great lengths
to wind around it. Who you are trying to fool here?
Reading luser responses such as this makes me wonder why I still bother
investing my free time for an answer, offering my knowledge and
experience for free.

Hurrah! A bright point in the thread. I have
been wondering the same thing about you, too.
[...]

I am sick and tired of your luser attitude. FOAD.
 
S

SAM

Le 6/21/10 2:01 PM, Csaba Gabor a écrit :
Thanks for this sample code. I am doing on
onclick what you are doing in onmousedown.
That gives me actual link openings in the current
window. However, as I read this code, it doesn't
cover me for right clicks.

there is nothing to have to do with right-click
since you did it for all mouse buttons with the mouse down
and my example covers any click than the left-one
(whom, by the fact, the right one wich is more difficult to detect as
not the same number with different navigators)


Just you would have to adapt, the file opened in the invisible iframe
would have to be only a very small php to save your clicks where you want.
The iframe has no other object than to do not get a JS error if there is
nothing to receive what the link (the windoxw.open) points to.

Certainly, instead to use an iFrame, wou could use the cookies.

<a href="http://google.com"
onmousedown="setCookie('myClicks',this.href)">google</a>

and no more trouble about left/right clicks, as all of them would be
detected together.
 
S

SAM

Le 6/21/10 3:12 PM, Thomas 'PointedEars' Lahn a écrit :
But HTTP proxying will not serve to detect if a new viewport was created to
view the referred resource. Nor can anything else, reliably.

Since, in fact and if I'd well understood, the purpose is only to know
if a click was made on a link, the problem about to know if same or new
window/tab receives the pointed file is off topic, is a bad question
from the OP.

On my idea a function to tell it (this link was clicked) to the server
in the 'onmousedown' would do the job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top