简体   繁体   English

右键单击启用“在新标签页/窗口中打开”

[英]Enable “open in new tab/window” in right click

My links are javascript functions which show a loader, then navigate to the target link: 我的链接是显示加载程序的javascript函数,然后导航到目标链接:

<script>
function go(url) {
  document.body.innerHTML = "some loader html";
  window.location = url;
}
</script>
<a href="javascript:go('test.php');">Click here</a>  

However this link wouldn't work when the user right clicks it and wants to navigate to test.php in a new tab. 但是,当用户右键单击该链接并想要导航到新标签中的test.php时,此链接将不起作用。

I want the link also to function when the user wants to open it in a new tab/window. 当用户要在新标签页/窗口中打开链接时,我希望该链接也起作用。 Is there a javascript/jquery way I can achieve this? 有没有可以实现此目的的javascript / jquery方法?

Thanks 谢谢

Your links should be links, not JavaScript functions. 您的链接应该是链接,而不是JavaScript函数。 Their primary purpose is navigation. 它们的主要目的是导航。 You can add the extra behavior later, in a click handler: 您可以稍后在点击处理程序中添加其他行为:

 document.body.addEventListener('click', evt => { const link = evt.target.closest('a.use-loader'); if (!link) return; evt.preventDefault(); document.body.innerHTML = '<h1 style="color:red">LOADING</h1>'; window.location.href = link.href; }); 
 <a href="https://example.com" class="use-loader"> This loads <em>really slow</em>, and it's my responsibility to fix that. </a> <br> <a href="https://example.org" class="use-loader">This one, too.</a> 

Or with jQuery: 或使用jQuery:

 $('body').on('click', 'a.use-loader', function () { document.body.innerHTML = '<h1 style="color:red">LOADING</h1>'; window.location.href = $(this).attr('href'); return false; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="https://example.com" class="use-loader"> This loads <em>really slow</em>, and it's my responsibility to fix that. </a> <br> <a href="https://example.org" class="use-loader">This one, too.</a> 

This way, the links still work for any agent that isn't running JavaScript, including browsers opening new tabs and users running NoScript. 这样,这些链接仍可用于任何未运行JavaScript的代理,包括打开新选项卡的浏览器和运行NoScript的用户。

Firstly, "My links are javascript functions which show a loader, then navigate to the target link" sounds like bad design decision. 首先,“我的链接是显示加载程序的javascript函数,然后导航到目标链接”听起来像是糟糕的设计决策。

To answer your question... window.open('test.php', '_blank'); 要回答你的问题... window.open('test.php', '_blank');

the second parameter will be the name of the target window see How to simulate target="_blank" in JavaScript 第二个参数将是目标窗口的名称,请参见如何在JavaScript中模拟target =“ _ blank”

The only way to get a right click 'open in a new window' is to have your tag like this 右键单击“在新窗口中打开”的唯一方法是使标签像这样

<a href="javascript:go('test.php');" target="_blank">Click here</a>

I highly recommend that you do not open the link using javascript, this is usally bad practice and you will most likely run into popup blocker issues. 我强烈建议您不要使用javascript打开链接,这通常是一种不好的做法,很可能会遇到弹出窗口阻止程序问题。 I would do this personally 我会亲自做

<a href="test" target="_blank">Click here</a>

Maybe this "loader" you want to show is supposed to imitate an AJAX loading approach where you load new HTML and insert it into your page without causing an actual page reload 也许您想要显示的“加载程序”应该模仿一种AJAX加载方法,在该方法中,您可以加载新的HTML并将其插入到页面中,而不会导致实际的页面重新加载

you might be interested in this How do I load the ajax data into a div with jquery? 您可能对此感兴趣: 如何使用jquery将ajax数据加载到div中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM