建立一个链接打开一个新窗口(不是标签)

585*_*nor 136 html

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

Iev*_*nov 233

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

<a href="print.html"  
    onclick="window.open('print.html', 
                         'newwindow', 
                         'width=300,height=250'); 
              return false;"
 >Print</a>
Run Code Online (Sandbox Code Playgroud)

  • 根据OP的"没有javascript"的定义(这个答案使用JS但不包括任何JS文件或<script>元素),这主要是正确的答案.我建议添加`href`,它鼓励浏览器正确设置链接样式:请参阅:http://jsfiddle.net/tobek/GPg6t/1/.`href`应该指向你用JS打开的实际链接,这使得它在语义上保持正确并且对搜索引擎更容易理解.`return false`阻止浏览器实际进入该链接. (6认同)
  • @KyleClegg我想你会在window.open()调用中添加"top"和"left"属性,但是由你决定屏幕的大小.我不相信有一种自动方式来集中一个新窗口.这个链接可能会有所帮助:[w3schools re window open](http://www.w3schools.com/jsref/met_win_open.asp) (4认同)
  • 此外,如果您不想重复链接,您可以使用:`onclick="window.open(this.href,'newwindow','width=1000,height=800'); return false;"` 这使用元素`&lt;a&gt;`的href (4认同)
  • 您也可以只使用 href,使用`&lt;a href="javascript:window.open('print.html', 'newwindow', 'width=300,height=250')"&gt;打印&lt;/a&gt;` (3认同)

Chr*_*oph 129

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

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

<a href="www.example.com/example.html" target="_blank">link text</a>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

  • 似乎在2015年没有工作.Safari 9.0 (3认同)

Cuz*_*uzi 29

我知道它有点老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));

});
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

但是,我建议您使用以下代码,如果您在移动设备中打开新标签中的链接(感谢其他问题中的zvona回答):

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;
}
Run Code Online (Sandbox Code Playgroud)

  • OP请求“没有javascript”,但jQuery基于javascript。 (2认同)