什么是打开新的浏览器窗口的最好方法? [英] What's the best way to open new browser window?

查看:161
本文介绍了什么是打开新的浏览器窗口的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,大多数链接应该留给最终用户决定如何打开,但我们不能否认,有次你几乎'必须'的力量进入一个新的窗口(例如,以保持数据在当前页面上的表单)。

I know that most links should be left up to the end-user to decide how to open, but we can't deny that there are times you almost 'have to' force into a new window (for example to maintain data in a form on the current page).

我想知道是共识是'最好'的方式来打开一个新的浏览器窗口的链接的内容。

What I'd like to know is what the consensus is on the 'best' way to open a link in a new browser window.

我知道< A HREF =网址目标=_空白> 超出。我也知道,< A HREF =#的onclick =window.open(URL);> 不理想的原因有多种。我也试着完全取代的东​​西像锚<跨度的onclick =window.open(URL);> ,然后在样式SPAN看起来像一个链接。

I know that <a href="url" target="_blank"> is out. I also know that <a href="#" onclick="window.open(url);"> isn't ideal for a variety of reasons. I've also tried to completely replace anchors with something like <span onclick="window.open(url);"> and then style the SPAN to look like a link.

一个解决方案,我倾向于是&LT; A HREF =URL相对=外部&GT; 和使用JavaScript来设置所有目标为_blank这些锚标有外部。

One solution I'm leaning towards is <a href="url" rel="external"> and using JavaScript to set all targets to '_blank' on those anchors marked 'external'.

是否有任何其他的想法?什么是好?我在寻找最符合XHTML和最简单的方式来做到这一点。

Are there any other ideas? What's better? I'm looking for the most XHTML-compliant and easiest way to do this.

更新:我说的目标=_空白,是一种没有没有,因为我读过的几个地方的,目标属性将被淘汰XHTML的。

UPDATE: I say target="_blank" is a no no, because I've read in several places that the target attribute is going to be phased out of XHTML.

推荐答案

我使用你所提出的最后一个方法。我想补充的rel =外在或类似的东西,然后使用jQuery通过各个环节进行迭代,并为它们分配一个click处理函数:

I am using the last method you proposed. I add rel="external" or something similar and then use jQuery to iterate through all links and assign them a click handler:

$(document).ready(function() {
  $('a[rel*=external]').click(function(){
    window.open($(this).attr('href'));
    return false; 
  });
});

我这找到最好的方法,因为:

I find this the best method because:


  • 这是非常明确的语义:你有一个链接到外部资源

  • 这是符合标准的

  • 它缓慢下降(你有定期的的href 属性很简单的链接)

  • 它仍然允许用户以中点击链接,并在新标签中打开它,如果他们希望

  • it is very clear semantically: you have a link to an external resource
  • it is standards-compliant
  • it degrades gracefully (you have a very simple link with regular href attribute)
  • it still allows user to middle-click the link and open it in new tab if they wish

这篇关于什么是打开新的浏览器窗口的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆