简体   繁体   English

如何在(Chrome、Mozilla)的新窗口中通过 ctrl + 单击打开链接?

[英]How to open the link by ctrl + click in the new window in (Chrome, Mozilla)?

I know that this is the default behavior of the browser (opening in a new tab), but are there any tricks to overcome them?我知道这是浏览器的默认行为(在新选项卡中打开),但是有什么技巧可以克服它们吗? Not using the other event handlers - such as onmousedown/onmouseup for <a> ?不使用其他事件处理程序 - 例如<a> onmousedown/onmouseup ? Trying to open with: window.open(url);试图打开: window.open(url);

It is a necessary to make it on the requirements specification.有必要使其符合需求规范。

    if (e.ctrlKey){
       window.open(url,'_blank')
     }

It will open the url in new tab.它将在新选项卡中打开 url。 Check for ctrl key in the click event检查点击事件中的ctrl

   $(".yourLink").bind('click', function(e){
         e.prevenDefault();
         if (e.ctrlKey){
       window.open(url,'_blank');
     }
   });

EDIT编辑

If you want to open in new window, specify width and height as follows如果要在新窗口中打开,请按如下方式指定宽度和高度

    window.open(url,'_blank', "height=255,width=255");

Actually, it is related to the CTRL key, seems like a bug in Chrome and it takes it as default behaviour even the e.preventDefault() was called.实际上,它与 CTRL 键有关,似乎是 Chrome 中的一个错误,即使调用了 e.preventDefault() 也将其作为默认行为。 In order to trick the browser you have to get out of the listener stack using a setTimeout function.为了欺骗浏览器,您必须使用 setTimeout 函数退出侦听器堆栈。

 $(".yourLink").bind('click', function(e){
    if (e.ctrlKey){
          var openWindow= function(){ 
              window.open(url,'name','width=' + screen.width + ',height=' + screen.height + ',location=yes,scrollbars=yes,status=yes;');
          }
          setTimeout(openWindow, 500); 
    }
 });

We have a custom javascript function in the "onclick" of an anchor tag in a product of ours.我们在我们的产品中的锚标记的“onclick”中有一个自定义的 javascript 函数。 This because we want to add additional parameters to the URL when navigating to the next screen.这是因为我们想在导航到下一个屏幕时向 URL 添加其他参数。

Anyway, we implemented "window.open()" while holding "Ctrl" to open a new tab.无论如何,我们在按住“Ctrl”打开一个新选项卡的同时实现了“window.open()”。 However, in Chrome, the javascript on the new tab was SO slow to execute and stayed that way the entire duration of that tab existing.但是,在 Chrome 中,新选项卡上的 javascript 执行速度非常慢,并且在该选项卡存在的整个持续时间内都保持这种状态。 The following solved the issue:以下解决了这个问题:

setTimeout(function() {window.open(url);}, 250);

I've adopted a montra: "when in doubt, setTimeout" in Chrome.我在 Chrome 中采用了一个 montra:“如有疑问,请设置超时”。 For some reason it solves so many wonky things.出于某种原因,它解决了很多不稳定的事情。 Anyway, hope this helps!无论如何,希望这会有所帮助!

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

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