首页 文章

Build 一个链接打开一个新窗口(而不是标签)[重复]

提问于
浏览
120

这个问题在这里已有答案:

有没有办法让链接打开一个新的浏览器窗口(而不是选项卡)而不使用JavaScript?

5 回答

  • 208

    浏览器控制了很多这个功能但是

    <a href="http://www.yahoo.com" target="_blank">Go to Yahoo</a>
    

    将尝试在新窗口中打开yahoo.com .

  • 26

    With pure HTML 你无法影响这一点 - 每个现代浏览器(=用户)都可以完全控制这种行为,因为它在过去被滥用了很多...

    anchor元素具有 target *属性 . 您要查找的值是 _blank ** .

    <a href="www.example.com/example.html" target="_blank">link text</a>
    

    它会打开一个新窗口(HTML4)或一个新的浏览上下文(HTML5) . 但是,现代浏览器中的浏览上下文主要是“新标签”而不是“新窗口” . 你没有影响,你不能“强迫”浏览器打开一个新窗口 .

    另一方面,可以通过javascript强制一个新窗口 - 请参阅下面的Ievgen答案的javascript解决方案 . 但是,请注意,通过javascript打开窗口(如果没有在锚点元素的onclick事件中完成)可能会被弹出窗口阻止程序阻止!

    (*)此属性可追溯到浏览器没有选项卡的时间,并且使用框架集是最先进的 . 与此同时,此属性的功能略有变化(请参阅MDN Docu

    (**)还有一些其他值没有多大意义(因为它们的设计考虑了框架集),如 _parent_self_top .

  • 117

    这将打开一个新窗口,而不是tab(使用JavaScript,但非常简洁):

    <a href="print.html"  
        onclick="window.open('print.html', 
                             'newwindow', 
                             'width=300,height=250'); 
                  return false;"
     >Print</a>
    
  • 1

    我知道它有点旧Q但是如果你通过搜索解决方案来到这里,所以我通过jquery得到了一个很好的

    jQuery('a[target^="_new"]').click(function() {
        var width = window.innerWidth * 0.66 ;
        // define the height in
        var height = width * window.innerHeight / window.innerWidth ;
        // Ratio the hight to the width as the user screen ratio
        window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
    
    });
    

    它将在新窗口中打开所有 <a target="_new">

    EDIT:

    1,我在原始代码中做了一些小改动现在它完全按照用户屏幕比例打开新窗口(对于风景桌面)

    但是,我建议您使用以下代码,如果您在移动设备中打开新标签中的链接(感谢zvona answer in other question):

    jQuery('a[target^="_new"]').click(function() {
        return openWindow(this.href);
    }
    
    
    function openWindow(url) {
    
        if (window.innerWidth <= 640) {
            // if width is smaller then 640px, create a temporary a elm that will open the link in new tab
            var a = document.createElement('a');
            a.setAttribute("href", url);
            a.setAttribute("target", "_blank");
    
            var dispatch = document.createEvent("HTMLEvents");
            dispatch.initEvent("click", true, true);
    
            a.dispatchEvent(dispatch);
        }
        else {
            var width = window.innerWidth * 0.66 ;
            // define the height in
            var height = width * window.innerHeight / window.innerWidth ;
            // Ratio the hight to the width as the user screen ratio
            window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
        }
        return false;
    }
    
  • 0

    你可以试试这个: -

    <a href="some.htm" target="_blank">Link Text</a>
    

    你也可以尝试这个: -

    <a href="some.htm" onclick="if(!event.ctrlKey&&!window.opera){alert('Hold the Ctrl Key');return false;}else{return true;}" target="_blank">Link Text</a>
    

相关问题