How can I disable HREF if onclick is executed?

JavascriptHtmlOnclickAnchorHref

Javascript Problem Overview


I have an anchor with both HREF and ONCLICK attributes set. If clicked and Javascript is enabled, I want it to only execute ONCLICK and ignore HREF. Likewise, if Javascript is disabled or unsupported, I want it to follow the HREF URL and ignore ONCLICK. Below is an example of what I'm doing, which would execute the JS and follow the link concurrently (usually the JS is executed and then the page changes):

<A HREF="http://example.com/no-js-login" ONCLICK="yes_js_login()">Log in</A>

what's the best way to do this?

I'm hoping for a Javascript answer, but I'll accept any method as long as it works, especially if this can be done with PHP. I've read "https://stackoverflow.com/questions/10712528/a-href-link-executes-and-redirects-page-before-javascript-onclick-function-is-ab" already, but it only delays HREF, but doesn't completely disable it. I'm also looking for something much simpler.

Javascript Solutions


Solution 1 - Javascript

You can use the first un-edited solution, if you put return first in the onclick attribute:

<a href="https://example.com/no-js-login" onclick="return yes_js_login();">Log in</a>

yes_js_login = function() {
     // Your code here
     return false;
}

Example: https://jsfiddle.net/FXkgV/289/

Solution 2 - Javascript

    yes_js_login = function() {
         // Your code here
         return false;
    }

If you return false it should prevent the default action (going to the href).

Edit: Sorry that doesn't seem to work, you can do the following instead:

<a href="http://example.com/no-js-login" onclick="yes_js_login(); return false;">Link</a>

Solution 3 - Javascript

Simply disable default browser behaviour using preventDefault and pass the event within your HTML.

<a href=/foo onclick= yes_js_login(event)>Lorem ipsum</a>

yes_js_login = function(e) {
  e.preventDefault();
}

Solution 4 - Javascript

<a href="http://www.google.com" class="ignore-click">Test</a>

with jQuery:

<script>
    $(".ignore-click").click(function(){
        return false;
    })
</script>

with JavaScript

<script>
        for (var i = 0; i < document.getElementsByClassName("ignore-click").length; i++) {
            document.getElementsByClassName("ignore-click")[i].addEventListener('click', function (event) {
                event.preventDefault();
                return false;
            });
        }
</script>

You assign class .ignore-click to as many elements you like and clicks on those elements will be ignored

Solution 5 - Javascript

You can use this simple code:

<a href="" onclick="return false;">add new action</a><br>

Solution 6 - Javascript

It's simpler if you pass an event parameter like this:

<a href="#" onclick="yes_js_login(event);">link</a>

function yes_js_login(e) {
    e.stopImmediatePropagation();
}

Solution 7 - Javascript

Try using javascript:void(0) as follows-

<a href="javascript:void(0)" onclick="...">Text</a>

Solution 8 - Javascript

This might help. No JQuery needed

<a href="../some-relative-link/file" 
onclick="this.href = 'https://docs.google.com/viewer?url='+this.href; this.onclick = '';" 
target="_blank">

This code does the following: Pass the relative link to Google Docs Viewer

  1. Get the full link version of the anchor by this.href
  2. open the link the the new window.

So in your case this might work:

<a href="../some-relative-link/file" 
onclick="this.href = 'javascript:'+console.log('something has stopped the link'); " 
target="_blank">

Solution 9 - Javascript

Solution 10 - Javascript

In my case, I had a condition when the user click the "a" element. The condition was:

If other section had more than ten items, then the user should be not redirected to other page.

If other section had less than ten items, then the user should be redirected to other page.

The functionality of the "a" elements depends of the other component. The code within click event is the follow:

var elementsOtherSection = document.querySelectorAll("#vehicle-item").length;
if (elementsOtherSection> 10){
     return true;
}else{
     event.preventDefault();
     return false;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKy.View Question on Stackoverflow
Solution 1 - Javascriptelproduc3rView Answer on Stackoverflow
Solution 2 - JavascriptChris BierView Answer on Stackoverflow
Solution 3 - JavascriptBuksyView Answer on Stackoverflow
Solution 4 - JavascriptAndrewView Answer on Stackoverflow
Solution 5 - JavascriptmishanonView Answer on Stackoverflow
Solution 6 - JavascriptTrigon219View Answer on Stackoverflow
Solution 7 - Javascriptuser3765825View Answer on Stackoverflow
Solution 8 - JavascriptmarloView Answer on Stackoverflow
Solution 9 - JavascriptBo JohansonView Answer on Stackoverflow
Solution 10 - JavascriptJorge Santos NeillView Answer on Stackoverflow