Forum Moderators: open

Message Too Old, No Replies

Disable Link After a Click

Disable Link After a Click

         

jeff5311

10:37 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



Can someone please tell me how to do this using javascript? I'm sure there's a simple solution... but I'm a javascript newb...

For instance, given the following link:

<a href="link.html">click here</a>

how do i allow a click the first time, yet disable it for future clicks from the same user in the same browser window.

I've tried disabling the link using the onclick event, but it seems to only grey out the link yet still allow clicks.

can anyone help?

thanks!
jeff

Span

11:23 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't even need JavaScript for that, it can be done with CSS:

a:visited {
display:none;
}

..but Jeff, don't be surprised if that's going to confuse your visitors so much that they will never visit your site again..

adni18

11:23 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

<a href="link.html" onclick="this.href='javascript:void(0)';this.disabled=1">click here</a>

Edit: whoops! Span, sorry bout that. Looks like we posted at the same time. :P

jeff5311

11:57 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



thanks for the replies guys!

however, i need the link to work the first time, but disabled on subsequent clicks. does this make sense?

anything else i should try?

Span

12:20 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well the question is, what's more annoying? A disabled link or a link that has completely disappeared after clicking on it?
A link that doesn't work after clicking on it once is going to make the visitor wonder what's going on.
A link that simply isn't there anymore is going to make the visitor wonder if he/she is gone mad.

No seriously, if I had to do this I would make the link disappear instead of disabling it (like in my previous post). Something that isn't there is less annoying than something that is there and doesn't work.

jeff5311

12:47 am on Jan 8, 2006 (gmt 0)

10+ Year Member



span,

thanks for the reply... i see your point...

how would a implement your solution via an inline style?

something like:

<a href="link.html" style="a:visited{display:none}">click here</a>

doesn't seem to work... unfortunately, i can't use an external style sheet...

Span

1:01 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, no, pseudo styles don't work inline. I've seen a working draft on the W3 site a few years ago but it could easily take 5 years before browsers are going to support that.
Can't you put it in the head of your page(s)? With a class assigned to the link?

a.disappear:visited {
display:none;
}

<a href="" class="disappear">Click once</a>

Key_Master

1:09 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html>
<head>
<script type="text/javascript">
function disableLink(url) {
document.write("<style type='text/css'>.disLink {display:none;}</style>");
location.href = url;
}
</script>
</head>
<body>
<a href="javascript:disableLink('http://www.example.com');" class="disLink">test</a>
</body>
</html>

Try this. It's untested in any browsers other than IE.

jeff5311

2:17 am on Jan 8, 2006 (gmt 0)

10+ Year Member



these are all such great suggestions!

as always, there's more to my story/problem...

the link i'm trying to keep from multiple clicks is being served in a banner on a banner ad. the banner ad is served on a PPC basis, hence the reason to only allow one click per time it's served.

unfortunately, i don't have control over the pages that the ad may be served on... so i don't think i can use an external style sheet (or could i?).

is there a way to increment a some sort of a counter to see if it's been clicked once already, and if so, hide the banner (or make it unclickable)?

any other suggestions?

thanks so much for all your help!

btw... the CSS solution only seems to work in IE for me...

adni18

3:46 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, this is an entirely different scenario. Is the frame (I assume it's a frame) URL on the same domain? If not, there is probably no solution.

jeff5311

4:28 am on Jan 8, 2006 (gmt 0)

10+ Year Member



yes... the ad's in an iframe, and can be served on any page/domain on the Internet... am I doomed?

Trace

3:48 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Just thinking out loud here, so I'm sorry in advance if this makes no sense...

Does the ad always appear in the same location?
If so, could you not just capture the mouse position when clicked and if it's in the banner area, make a layer appear covering the banner?

It could either be a transparent layer, or a layer saying thanks for the clicks... or you could even serve another banner.

a1call

4:04 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Hi,

This may or may not work, but as a brainstorm. How about having an invisible gif image cover the banner after the first click using js.
Notes:
-This might be against the TOS of the banner provider. I am sure it's against adsense's.
- Once clicked, I assume one has to click on the browsers back button, which usually serves the cached version and thus no distinction can be made with first or later clicks. The workaround might be to have the page refresh automatically and at a fast rate.

jeff5311

4:29 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



thanks guys for your helps and brainstorming...

i couldn't figure out a way to screen multiple clicks using javascript, so i had to go to server side coding.

regardless, thanks again!

a1call

7:57 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Hi jeff5311,

The way I see it the cache factor would still be an issue. Unless your page is constantly refreshing like the "recent posts" page here, multiple clicks can happen. May be I misunderstand and you are actually referring to more than one page. In any case I'm glad the issue is resolved.