【问题标题】:javascript window.open() method still open in new tab while user hold the ctrl button当用户按住 ctrl 按钮时,javascript window.open() 方法仍然在新选项卡中打开
【发布时间】:2023-04-04 16:36:01
【问题描述】:

我正在使用以下代码在弹出窗口中打开试卷,它工作正常,但是当用户按住 ctrl 按钮并单击开始按钮时,它仍然在新选项卡中打开,我正在使用以下代码

$(document).ready(function(){
        $('#start').on('click', function(){
     
            window.open ('webURL',"mywindow", "width="+screen.availWidth+",height="+screen.availHeight, "status=1,toolbar=0");
        });
    });

【问题讨论】:

  • 因为你没有在那里检查 CTRL。如果按下 CTRL,您是否希望不打开窗口?
  • 是的,但我不知道该怎么做,请你告诉我怎么做。我是新来的
  • 你有一个错字:window.open ('webURL',"mywindow", "width="+screen.availWidth+",height="+screen.availHeight+",status=1,toolbar=0"); 或只是window.open ('webURL',"mywindow", "status,width="+screen.availWidth+",height="+screen.availHeight);
  • 有趣,我不知道 Chrome 有这个功能 ;-)。您目前可以通过在一个小的超时(例如 10 毫秒)中包装您的 open 调用来解决它,但我不确定它会工作多长时间,也不确定所有 UA 是否都会接受调用。
  • 它使用 10 毫秒的超时。感谢大家的帮助

标签: javascript jquery window.open


【解决方案1】:

检查event 对象的ctrlKey 属性。

    $(document).ready(function(){
        $('#start').on('click', function(event){
            if(event.ctrlKey)
               window.open ('webURL',"mywindow", "width="+screen.availWidth+",height="+screen.availHeight+ ",status");
        });
    });

【讨论】:

  • OP 如果按住 ctrl 也想弹出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2022-07-30
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多