Customize right click anchor tag to open link in new tab


usman610

I have the following code to open a specific right click menu for <a>an element .

On any link I right click and click to open in a new tab, it only opens the first link. Counters in java-script are related to my inability to do so.

hrefI want to open each link on a right click so it will open the link in new tab by getting the link <a>element.

If anyone has another script, which of course should be better for this purpose, please share with me.

thanks.

Get link from database

$i =1;
$get_newbooks   =   mysqli_query($dba, "select * from mynewbooks
where status = 1
");
while ($thisbook        =   mysqli_fetch_array($get_newbooks)) {
?>
    <a href="<?php echo $thisbook['link']; ?>" id="openinnewtabt<?php echo $i++; ?>">
    <?php echo $thisbook['name']; ?>
    </a>
}

right-click menu for labels

<div class="hide" id="rmenu">
  <ul>
    <hr style="margin-top: 3px; margin-bottom: 0px; border: 1px solid black;">
    <li>
        <button style="text-align: center; color: black; width: 150px;"
        class="opennewtabt item copy-button">
        Open Link In New Tab
        </button>
    </li>
    <hr style="margin-top: 3px; margin-bottom: 0px; border: 1px solid black;">
  </ul>
</div>

CSS

.show {
  z-index: 10000000000000;
  position: absolute;
  background-color: #C0C0C0;
  border: 1px solid grey;
  padding: 2px;
  display: block;
  margin: 0;
  list-style-type: none;
  list-style: none;
}

.hide {
  display: none;
}

.show li {
  list-style: none;
}

.show a {
  border: 0 !important;
  text-decoration: none;
}

.show a:hover {
  text-decoration: underline !important;
}

Javascript

$(document).ready(function() {
    $('body').on('contextmenu', 'a', function() {
    var counter = 1;
    $('.opennewtabt').on('click', function() {
        var link = $('#openinnewtabt'+counter).attr('href');
        window.open(link, '_blank').focus();
        return false;
    });
      document.getElementById("rmenu").className = "show";
      document.getElementById("rmenu").style.top = mouseY(event) + 'px';
      document.getElementById("rmenu").style.left = mouseX(event) + 'px';

      window.event.returnValue = false;
    });
});
// this is from another SO post...  
$(document).bind("click", function(event) {
  document.getElementById("rmenu").className = "hide";
});

function mouseX(evt) {
  if (evt.pageX) {
    return evt.pageX;
  } else if (evt.clientX) {
    return evt.clientX + (document.documentElement.scrollLeft ?
      document.documentElement.scrollLeft :
      document.body.scrollLeft);
  } else {
    return null;
  }
}

function mouseY(evt) {
  if (evt.pageY) {
    return evt.pageY;
  } else if (evt.clientY) {
    return evt.clientY + (document.documentElement.scrollTop ?
      document.documentElement.scrollTop :
      document.body.scrollTop);
  } else {
    return null;
  }
}
Louis Patrice Bassett

You set the clickhandler in the handler contextmenu...

Instead, you should put it outside and pass hrefit to the button inside the context menu.
This can be done using .data () .

So you just don't need any idlinks...and there are none counter.

$(document).ready(function() {

  // Context menu open
  $("body").on("contextmenu", "a", function() {

    // pass the href to the context menu button
    $(".opennewtabt").data("href", this.href)
    $(".opennewtabt").text($(this).text() + " in a new window")

    // document.getElementById("rmenu").className = "show";
    // document.getElementById("rmenu").style.top = mouseY(event) + "px";
    // document.getElementById("rmenu").style.left = mouseX(event) + "px";
    
    // With jQuery, the 3 lines above can be writen like this
    $("#rmenu").removeClass("hide").addClass("show").css({"top":mouseY(event) + "px", "left":mouseX(event) + "px"})
    
    window.event.returnValue = false;
  });

  // Context menu click handler
  $(".opennewtabt").on("click", function(e) {
    var link = $(e.target).data("href")
    window.open(link, "_blank").focus();
    return false;
  });
});


// this is from another SO post...
$(document).bind("click", function(event) {
  document.getElementById("rmenu").className = "hide";
});

function mouseX(evt) {
  if (evt.pageX) {
    return evt.pageX;
  } else if (evt.clientX) {
    return (
      evt.clientX +
      (document.documentElement.scrollLeft ?
        document.documentElement.scrollLeft :
        document.body.scrollLeft)
    );
  } else {
    return null;
  }
}

function mouseY(evt) {
  if (evt.pageY) {
    return evt.pageY;
  } else if (evt.clientY) {
    return (
      evt.clientY +
      (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop)
    );
  } else {
    return null;
  }
}
.show {
  z-index: 10000000000000;
  position: absolute;
  background-color: #c0c0c0;
  border: 1px solid grey;
  padding: 2px;
  display: block;
  margin: 0;
  list-style-type: none;
  list-style: none;
}

.hide {
  display: none;
}

.show li {
  list-style: none;
}

.show a {
  border: 0 !important;
  text-decoration: none;
}

.show a:hover {
  text-decoration: underline !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="http://example.com">
  test link 1
</a><br>

<a href="http://test.com">
  test link2
</a><br>

<a href="http://hello.com">
  test link3
</a><br>

<div class="hide" id="rmenu">
  <ul>
    <hr style="margin-top: 3px; margin-bottom: 0px; border: 1px solid black;">
    <li>
      <button style="text-align: center; color: black; width: 150px;" class="opennewtabt item copy-button">
        Open Link In New Tab
      </button>
    </li>
    <hr style="margin-top: 3px; margin-bottom: 0px; border: 1px solid black;">
  </ul>
</div>

Since window.openit's not allowed in SO snippets, check out the CodePen

Related


Right click and save link to open in new tab

Dishu I added a custom right click menu using jquery in my project, now what I want to achieve is: -> If anyone right clicks on the link, the option will say "Open link in new tab", when that option is selected, it will open in a new tab with that link. -> Oth

Open tab with anchor tag link?

gg93 So I have two tags on the page with anchor tags - I navigate to them from the homepage using the anchor on the homepage menu item. I've used the active class and aria-expanded="true" to get the actual tab working, but I can't get the appropriate tab from

Open tab with anchor tag link?

gg93 So I have two tags on the page with anchor tags - I navigate to them from the homepage using the anchor on the homepage menu item. I've used the active class and aria-expanded="true" to get the actual tab working, but I can't get the appropriate tab from

Get anchor tag link to open in new tab when using Tag Helper

Dinosaur Using the new tag helper in asp.net-core-mvc <a class="custom-social-icon" asp-area="" asp-protocol="https" asp-host="www.youtube.com/mysite" > <img src="~/images/youtube.gif" alt="youtube_logo" /> </a> Is there a setting in the new tab helper to

Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it will open the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink

Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it opens the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink:hre

Right click to enable "open in new tab/window"

Michael Chodakis My link is a javascript function that shows the loader and then navigates to the target link: <script> function go(url) { document.body.innerHTML = "some loader html"; window.location = url; } </script> <a href="javascript:go('test.php');"

Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it opens the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink:hre

Right click to enable "open in new tab/window"

Michael Chodakis My link is a javascript function that shows the loader and then navigates to the target link: <script> function go(url) { document.body.innerHTML = "some loader html"; window.location = url; } </script> <a href="javascript:go('test.php');"

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Bind click event to open link in new tab

123 I'm trying to open all links in a modal in a new tab, manipulating the DOM is not as easy as I thought due to the limitations of using angular. Is there a way to bind a click event to a link and then make the click event open a new tab? I have something li

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

"Open in new tab" click link does not open href value

99 encoder I am using Vue2 and I have and <a>markup . This works as expected with a left click (it's calling a method), but if I do a , which is the open value in this case, it 's wrong.href='#'@click.prevent="someMethod()" right click -> open in new tabhref W

Click the link to open the tab

Shakamar I want to open a tab when a user clicks an image link on the following website : http://m.theseolounge.co.uk/index.php/jadever-jad-jce-counting-scale.html The code for the tab is as follows: <ul class="tabs clearer"> <li id="tab-additional"><a href=

Open link in new tab

no mice here I need to open a link <a>in a new tab/window 's tab, do something there and then close it. html code: <body> <a id="uniqueid" href="somelink">I need to open this in a new tab</a> </body> python code: from selenium import webdriver driver = w

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive which generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is ope

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing happens. PS I am using pyQt5.6 I've tried it on Linux centOs,

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open