stop opening a new tab on click mouse middle button





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have on anchor tag in htm. when I click mouse middle button on anchor tag then new tab is opened.
But I want to stop it's working. I tried lot of tricks in javascript but it is not working. Can anybody have solution for that?



<a class="tag" href="www.google.com>google</a>


I don't want to open a new tab by clicking with mouse middle button over anchor tag having class (tag)...
prevent mouse middle button to open new tab on anchor tab with a particular id.










share|improve this question




















  • 2





    what you have tried till now?

    – Ranjit Singh
    Jun 2 '16 at 11:44











  • <html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

    – RohanArihant
    Jun 2 '16 at 11:45











  • mouseup might be too late

    – GôTô
    Jun 2 '16 at 11:46






  • 6





    Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

    – Matt Lishman
    Jun 2 '16 at 11:48








  • 2





    Don't do it, I would leave your site if middle click didn't open the page in a new tab

    – Juan Mendes
    Jun 2 '16 at 11:54


















0















I have on anchor tag in htm. when I click mouse middle button on anchor tag then new tab is opened.
But I want to stop it's working. I tried lot of tricks in javascript but it is not working. Can anybody have solution for that?



<a class="tag" href="www.google.com>google</a>


I don't want to open a new tab by clicking with mouse middle button over anchor tag having class (tag)...
prevent mouse middle button to open new tab on anchor tab with a particular id.










share|improve this question




















  • 2





    what you have tried till now?

    – Ranjit Singh
    Jun 2 '16 at 11:44











  • <html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

    – RohanArihant
    Jun 2 '16 at 11:45











  • mouseup might be too late

    – GôTô
    Jun 2 '16 at 11:46






  • 6





    Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

    – Matt Lishman
    Jun 2 '16 at 11:48








  • 2





    Don't do it, I would leave your site if middle click didn't open the page in a new tab

    – Juan Mendes
    Jun 2 '16 at 11:54














0












0








0


1






I have on anchor tag in htm. when I click mouse middle button on anchor tag then new tab is opened.
But I want to stop it's working. I tried lot of tricks in javascript but it is not working. Can anybody have solution for that?



<a class="tag" href="www.google.com>google</a>


I don't want to open a new tab by clicking with mouse middle button over anchor tag having class (tag)...
prevent mouse middle button to open new tab on anchor tab with a particular id.










share|improve this question
















I have on anchor tag in htm. when I click mouse middle button on anchor tag then new tab is opened.
But I want to stop it's working. I tried lot of tricks in javascript but it is not working. Can anybody have solution for that?



<a class="tag" href="www.google.com>google</a>


I don't want to open a new tab by clicking with mouse middle button over anchor tag having class (tag)...
prevent mouse middle button to open new tab on anchor tab with a particular id.







javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:03









Cœur

19.3k10116155




19.3k10116155










asked Jun 2 '16 at 11:41









RohanArihantRohanArihant

74721124




74721124








  • 2





    what you have tried till now?

    – Ranjit Singh
    Jun 2 '16 at 11:44











  • <html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

    – RohanArihant
    Jun 2 '16 at 11:45











  • mouseup might be too late

    – GôTô
    Jun 2 '16 at 11:46






  • 6





    Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

    – Matt Lishman
    Jun 2 '16 at 11:48








  • 2





    Don't do it, I would leave your site if middle click didn't open the page in a new tab

    – Juan Mendes
    Jun 2 '16 at 11:54














  • 2





    what you have tried till now?

    – Ranjit Singh
    Jun 2 '16 at 11:44











  • <html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

    – RohanArihant
    Jun 2 '16 at 11:45











  • mouseup might be too late

    – GôTô
    Jun 2 '16 at 11:46






  • 6





    Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

    – Matt Lishman
    Jun 2 '16 at 11:48








  • 2





    Don't do it, I would leave your site if middle click didn't open the page in a new tab

    – Juan Mendes
    Jun 2 '16 at 11:54








2




2





what you have tried till now?

– Ranjit Singh
Jun 2 '16 at 11:44





what you have tried till now?

– Ranjit Singh
Jun 2 '16 at 11:44













<html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

– RohanArihant
Jun 2 '16 at 11:45





<html> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/…> <body> <a id="my" href="google.com">Clicker</a> <div id="log"></div> <script> $('a').mouseup(function (e) { e.preventDefault(); e.stopPropagation(); }); $(document).click(function (e) { if (e.which == 2) { e.preventDefault(); return false; } }); </script> </body> </html>

– RohanArihant
Jun 2 '16 at 11:45













mouseup might be too late

– GôTô
Jun 2 '16 at 11:46





mouseup might be too late

– GôTô
Jun 2 '16 at 11:46




6




6





Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

– Matt Lishman
Jun 2 '16 at 11:48







Personally, unless you have a really good reason, I wouldn't do this. It's really annoying to hijack the users expected behaviour.

– Matt Lishman
Jun 2 '16 at 11:48






2




2





Don't do it, I would leave your site if middle click didn't open the page in a new tab

– Juan Mendes
Jun 2 '16 at 11:54





Don't do it, I would leave your site if middle click didn't open the page in a new tab

– Juan Mendes
Jun 2 '16 at 11:54












2 Answers
2






active

oldest

votes


















1














I found solution as per my requirement



<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
$(function(){
$(document).on("click", function(e){
if($(e.target).is("#google") && e.button===1)
e.preventDefault()
})
})
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
</body>
</html>


fiddle link






share|improve this answer































    0














    I agree with @Matt Lishman in the comments: don't.



    But to give you a solution:



    Your code is almost right. Only (as @GoTo says), mouseup is to late.
    When you listen for click events you can check the which property on the event object. The which is 2 when you click with the scrollwheel.



    So, if which === 2, preventDefault



    https://jsfiddle.net/k3o5pt6c/






    share|improve this answer
























    • i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

      – RohanArihant
      Jun 2 '16 at 12:16














    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37590837%2fstop-opening-a-new-tab-on-click-mouse-middle-button%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I found solution as per my requirement



    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script>
    $(function(){
    $(document).on("click", function(e){
    if($(e.target).is("#google") && e.button===1)
    e.preventDefault()
    })
    })
    </script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
    <a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
    </body>
    </html>


    fiddle link






    share|improve this answer




























      1














      I found solution as per my requirement



      <html>
      <head>
      <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
      <script>
      $(function(){
      $(document).on("click", function(e){
      if($(e.target).is("#google") && e.button===1)
      e.preventDefault()
      })
      })
      </script>
      <meta charset=utf-8 />
      <title>JS Bin</title>
      </head>
      <body>
      <a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
      </body>
      </html>


      fiddle link






      share|improve this answer


























        1












        1








        1







        I found solution as per my requirement



        <html>
        <head>
        <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
        <script>
        $(function(){
        $(document).on("click", function(e){
        if($(e.target).is("#google") && e.button===1)
        e.preventDefault()
        })
        })
        </script>
        <meta charset=utf-8 />
        <title>JS Bin</title>
        </head>
        <body>
        <a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
        </body>
        </html>


        fiddle link






        share|improve this answer













        I found solution as per my requirement



        <html>
        <head>
        <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
        <script>
        $(function(){
        $(document).on("click", function(e){
        if($(e.target).is("#google") && e.button===1)
        e.preventDefault()
        })
        })
        </script>
        <meta charset=utf-8 />
        <title>JS Bin</title>
        </head>
        <body>
        <a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
        </body>
        </html>


        fiddle link







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 3 '16 at 12:03









        RohanArihantRohanArihant

        74721124




        74721124

























            0














            I agree with @Matt Lishman in the comments: don't.



            But to give you a solution:



            Your code is almost right. Only (as @GoTo says), mouseup is to late.
            When you listen for click events you can check the which property on the event object. The which is 2 when you click with the scrollwheel.



            So, if which === 2, preventDefault



            https://jsfiddle.net/k3o5pt6c/






            share|improve this answer
























            • i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

              – RohanArihant
              Jun 2 '16 at 12:16


















            0














            I agree with @Matt Lishman in the comments: don't.



            But to give you a solution:



            Your code is almost right. Only (as @GoTo says), mouseup is to late.
            When you listen for click events you can check the which property on the event object. The which is 2 when you click with the scrollwheel.



            So, if which === 2, preventDefault



            https://jsfiddle.net/k3o5pt6c/






            share|improve this answer
























            • i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

              – RohanArihant
              Jun 2 '16 at 12:16
















            0












            0








            0







            I agree with @Matt Lishman in the comments: don't.



            But to give you a solution:



            Your code is almost right. Only (as @GoTo says), mouseup is to late.
            When you listen for click events you can check the which property on the event object. The which is 2 when you click with the scrollwheel.



            So, if which === 2, preventDefault



            https://jsfiddle.net/k3o5pt6c/






            share|improve this answer













            I agree with @Matt Lishman in the comments: don't.



            But to give you a solution:



            Your code is almost right. Only (as @GoTo says), mouseup is to late.
            When you listen for click events you can check the which property on the event object. The which is 2 when you click with the scrollwheel.



            So, if which === 2, preventDefault



            https://jsfiddle.net/k3o5pt6c/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 2 '16 at 11:57









            PimmolPimmol

            1,6381412




            1,6381412













            • i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

              – RohanArihant
              Jun 2 '16 at 12:16





















            • i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

              – RohanArihant
              Jun 2 '16 at 12:16



















            i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

            – RohanArihant
            Jun 2 '16 at 12:16







            i agree with you but for a particular reason i want to stop it to open a new tab.. can anybody have solution i have an working example js.do/rohanarihant/mousemiddle1 but i stopped working of all the anchor tag but i want to stop only one by it's id(my) –

            – RohanArihant
            Jun 2 '16 at 12:16




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37590837%2fstop-opening-a-new-tab-on-click-mouse-middle-button%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith