Solved

Disable hyperlink after clicking it

Posted on 2006-06-21
8
4,970 Views
Last Modified: 2012-06-27
I have a hyper link defined as follows:

<a href='...' id='submitButton'><img src="img/submit.gif" border="0"></a>

When the link is clicked, I would like to disable this link. Can it be done?

Thanks!
0
Comment
Question by:yongsing
8 Comments
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 16950504
Hi yongsing,

it is not possible to disable hyperlink, but you can hide hyperlink after click on it once.

by document.all.hyperlinkname.style.display = 'none'
and for showing it back document.all.hyperlinkname.style.display = ''


Regards,
NetSwap
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16950679
It is possible.
Like this:



<a href='http://www.experts-exchange.com/Q_21893830.html' id='submitButton' onClick="this.onclick=function(){return false}" target="EE"><img src="img/submit.gif" border="0"></a>

The target is only for demo. It works also for target="_self".


0
 
LVL 7

Expert Comment

by:geordie007
ID: 16950700

Netswap is right, but beware. the document.all method is an old internet explorer reference. nowadays, all modern browsers - including internet explorer - use document.getElementById. using your example, you could do this:

document.getElementById('submitButton').style.display = 'none';

this would remove it completely though, so you would get document flow issues, as any content below it would move up, any content to the right, would move across, etc. to combat that you could say:

document.getElementById('submitButton').style.visibility= 'hidden';

of course, you may want to keep the image there, but simply make it so that it didn't work anymore. to do this, you could change the href of the link, like this:

<script type="text/javascript">
var clicked = false;
function isClicked(){
      if(clicked){
      document.getElementById('submitButton').href = "javascript:;";
      }
      clicked = true;
}
</script>

<a href="(MY ORIGINAL LINK OR FUNCTION)" onclick="isClicked()" id="submitButton"><img src="img/submit.gif" border="0"></a>

the first time it's clicked, clicked is false, so it doesn't change the href, and your original action happens. the second time its clicked, clicked is now true, so it sets your href to "javascript:;", which is the equivalent of "do nothing at all".

hope this helps.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 30

Expert Comment

by:callrs
ID: 16950783
Just created & tested this. It works! : )
change http://www.google.com to the link you want your user to go to.

<a href=""' id='submitButton'  onclick="this.outerHTML='<img src=img/submit.gif border=0>';window.open('http://www.google.com');"><img src="img/submit.gif" border="0"></a>
0
 
LVL 30

Accepted Solution

by:
callrs earned 30 total points
ID: 16950792
Even better:
<a href=""' id='submitButton'  onclick="this.outerHTML=this.innerHTML;window.open('http://www.google.com');"><img src="img/submit.gif" border="0"></a>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16950977
document.all and this.outerHTML is IE only.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16952923
I like Zvonko's since it is cross browser and quite elegant :D *bows down* heh (ok, this comment is probably just wasted space, but I did really like it; I would've never thought to set its onclick to return false, lol)

Ted
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16955060
Thanks Ted :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jquery datable hidden section doesn't submit 2 15
Asp in script 6 39
Google Analytics setup 5 51
Help with Javascript to round decimal points to 2 13 18
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question