473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Change link URL on click

Hi!

I am trying to implement link click counter so that user
can still bookmark the link (I rather see that click is
not counted than user have "problems").

<a href="http://www.google.com/"
onClick="javascript:document.location.replace('htt p://www.mypage.com
/cgi-bin/count?http://www.google.com/'); return false;">Google</a>

I have tryed this and it works. But the problem is that it
does not work on "open in new window" or "open in new tab"
click on a link.

Any ideas?
Mike
Jul 23 '05 #1
7 19243
On Thu, 08 Apr 2004 01:15:17 +0200, Mike Mimic <pp****@yahoo.com> wrote:
I am trying to implement link click counter so that user
can still bookmark the link (I rather see that click is
not counted than user have "problems").

<a href="http://www.google.com/"
onClick="javascript:document.location.replace('htt p://www.mypage.com
/cgi-bin/count?http://www.google.com/'); return false;">Google</a>

I have tryed this and it works. But the problem is that it
does not work on "open in new window" or "open in new tab"
click on a link.

Any ideas?
Use

<a href="http://www.mypage.com/cgi-bin/count?http://www.google.com/"Google</a>


perhaps?

When opening in a new window, browsers only use the href attribute (in my
experience), and that attribute must only contain "standard" URIs (read:
no "javascript:...").

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2
Hi!

Michael Winter wrote:
<a href="http://www.mypage.com/cgi-bin/count?http://www.google.com/"
>Google</a>


perhaps?


The problem with this is that if a user bookmark the link (right click
on a link -> add to bookmark) he will not get correct link and every
time he will use the bookmark counter will increase.
Mike
Jul 23 '05 #3
On Thu, 08 Apr 2004 13:31:11 +0200, Mike Mimic <pp****@yahoo.com> wrote:
Michael Winter wrote:
<a href="http://www.mypage.com/cgi-bin/count?http://www.google.com/"
>Google</a>


perhaps?


The problem with this is that if a user bookmark the link (right click
on a link -> add to bookmark) he will not get correct link and every
time he will use the bookmark counter will increase.


Your original link, or the one above, are your only options as I see them.
That, or abandoning it completely. The choice is yours.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
Michael Winter wrote:
<snip>
Your original link, or the one above, are your only options as I see
them. That, or abandoning it completely. The choice is yours.


It should be possible to modify the HREF in the onclick handler so the
original HREF is always the real HREF but a click action preppends the
counter page (with no return value so that the new HREF is followed).
That should work OK with javascript disabled, right clicking, drag and
drop and bookmaking (though some testing would be a good idea (and maybe
ECMA 262 3rd ed. - encodeURIComponent - instead of - escape -, where
supported?):-

<a href="http://www.google.com/"
onClick="this.href =
'http://www.mypage.com/cgi-bin/count?'+
escape(this.href);">Google</a>

Richard.
Jul 23 '05 #5
On Thu, 8 Apr 2004 14:35:27 +0100, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:

[snip]
It should be possible to modify the HREF in the onclick handler so the
original HREF is always the real HREF but a click action preppends the
counter page (with no return value so that the new HREF is followed).
That should work OK with javascript disabled, right clicking, drag and
drop and bookmaking (though some testing would be a good idea (and maybe
ECMA 262 3rd ed. - encodeURIComponent - instead of - escape -, where
supported?):-

<a href="http://www.google.com/"
onClick="this.href =
'http://www.mypage.com/cgi-bin/count?'+
escape(this.href);">Google</a>


That is, in effect, the same as the original link the OP posted. The
problem with that was right-clicking, or middle-clicking, to open the
destination in a new window won't trigger the onclick event and pass the
request through the OP's counter.

An interesting approach, though.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6
Hi!

Michael Winter wrote:
That is, in effect, the same as the original link the OP posted. The
problem with that was right-clicking, or middle-clicking, to open the
destination in a new window won't trigger the onclick event and pass the
request through the OP's counter.
Sadly. It really does not work any better.
An interesting approach, though.


Indeed.

I have discovered that document.location.replace is not a good thing
because "back" button does not work than (as replace is meant to do
this). But location.href = ... solves the problem.

Anyway. I will probably use Richard's idea.
Mike
Jul 23 '05 #7
Michael Winter wrote:
Richard Cornford wrote:
<a href="http://www.google.com/"
onClick="this.href =
'http://www.mypage.com/cgi-bin/count?'+
escape(this.href);">Google</a>


That is, in effect, the same as the original link the OP posted. ...

<snip>

Yes you are right, it won't behave much differently. It probably is more
reliable to let clicking on the link do the navigation rather than
scripting the - location - object, and it always makes more sense to me
to have the URL in the HREF and refer to - this.href - in the event
handlers, as that lets you use the same code for all links and not worry
about updating two URLs whenever anything changes.

I don't think there will be a way of doing what the OP wants because it
would never be possible to tell when a user of the context menu was
selecting bookmaking or opening in a new tab/window (or doing something
else entirely). It might be possible to use the mousedown event to
change the URL (in addition to onclick, to cover keyboard navigation,
and with more elaborate code to prevent preppending the URL twice) and
attempt to pick up the context menu a activity that way, but that will
end up possibly bookmaking the counter URL.

Richard.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Adams | last post by:
As well known I could specify the text color in the body tag like: <BODY TEXT=WHITE LINK=WHITE VLINK=RED ALINK=WHITE> What I want to achieve now is that always (!) the text of the last visited...
5
by: Matt | last post by:
I created 3 hyperlinks, when the user click each link, it will change the color of the text of a link. For example, when user clicks Link1, text Link1 will become red color, but Link2 and Link3...
2
by: Ben | last post by:
Hi. I have a button that change a number of images src's when I click a button. The src's are stored in an array and I just use document.src=pics to change the src of the image. However I want...
11
by: Yeah | last post by:
I have a multiple choice quiz where I would like to use CSS to change the color of the answers upon clicking them. I would like to present the right and wrong answers up front, rather than direct...
6
by: Karl | last post by:
Hi, Ok so on a given page I have 4 text links: see it in black see it in blue see it in red see it in green using the standard swap image behavior, clicking on one of the above links
2
by: rich | last post by:
I am working on a page with a right column that I want to use for navigation. In this right column I am using the below code to set a value for the link. I am using the variable $test right now....
5
by: james.calhoun | last post by:
I feel like this should be really easy... I want a hidden field in a form to have its value defined when someone clicks on a link. So if they click on link "A" the value of the hidden field...
22
by: subashinicse | last post by:
hi to everybody, am new to this forum.. am working with J2ee,i have a jsp page where i have some links,i want to change the link color when it is active,and it has to turn back to original color...
1
by: Adrock952 | last post by:
I have a link on my site which obviously says "Login" where users log in. I would like that link to be changed to "Logout" when the user has successfully logged in and the session has been created...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.