1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

window.open with middle mouse click

Discussion in 'JavaScript' started by Alex100, Jun 19, 2020.

  1. #1
    I'm using the code below to open a button link using both the left and the middle mouse buttons. As it is now, if you press the middle button, a new tab opens, and then you have to manually click that tab to see its content. I'd like to point out this only happens in FireFox.

    In Chrome however, the new tab is opened and then also brought into focus right after you middle-click the button.

    Is there a quick way to make the script work the same way in FireFox as it does in Chrome?

    Thank you,
    Alex

    CODE SOURCE: https://stackoverflow.com/questions/40946442/how-can-i-make-a-javascript-link-openable-via-middle-click

    
    <html>
      <head>
        <script language="javascript" type="text/javascript">
          function open_on_middle_click(event,url,target) {
            var x = event.buttons;
            if (x == 4) {
              window.open(url, target);
            }
          }
        </script>
      </head>
      <body>
        <div onclick="window.open('https://www.google.com')" onmousedown="open_on_middle_click(event,'https://www.google.com','_blank')" style="width: 120px; height: 20px; font-family: arial; border: 1px solid black; text-align: center; cursor: pointer;">button</div>
      </body>
    </html>
    
    Code (markup):
     
    Last edited: Jun 19, 2020
    Alex100, Jun 19, 2020 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    598
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    I thought that window.focus() after window.open() could work but it doesn't. If you control opened URL too, you can try to implement a Window Rollover technique, but there will be some flickering because it closes and reloads the first page.
     
    wmtips, Jun 19, 2020 IP
  3. Stephtrap

    Stephtrap Peon

    Messages:
    11
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    3
    #4
    Is there any way you could implement some sort of alt+tab command so the window will not be in focus then the script of alt+tab will bring it to the forefront. Note this is above my paygrade but every idea counts and I believe its a solid one.
     
    Stephtrap, Jun 20, 2020 IP
  4. Alex100

    Alex100 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Thank you for your suggestion!

    Alex
     
    Alex100, Jun 20, 2020 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Generally with many adblockers killing off scripted window.open, this is bad... but then opening new windows whether the user wants it or not is also bad since it prevents conventional navigation. If someone WANTS a new window let them middle-click or ctrl-click, don't try to shove it down the user's gullet. That's why target="" was deprecated in 4 Strict and why it's unbelievably DUMBASS that it's back in 5.

    Also, this isn't 1997, we don't say lang="JavaScript" anymore. This also isn't 2011, we don't say type="text/JavaScript" anymore either. Likewise one should NOT be using a DIV as a click hook, you should NOT be attaching events with onevent attributes in the markup, you should NOT be slopping static style="" crap into the markup like that, etc, etc, etc...

    Just make it a normal blasted <a>nchor and be done with it.
     
    deathshadow, Jun 21, 2020 IP