javascript html 在新目标和焦点上打开一个 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4546528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html open a url on new target and focus
提问by ctrl-alt-delor
I am trying to fix a web site.
我正在尝试修复一个网站。
It opens a help page in a new window/tab via <a href="..." target="help">(no other frame has this name)
它通过<a href="..." target="help">(没有其他框架具有此名称)在新窗口/选项卡中打开一个帮助页面
This works well the first time opening a new window/tab, for the help. But on subsequent clicks the window/tab is loaded but remains hidden.
这在第一次打开新窗口/选项卡时效果很好,以获得帮助。但在随后的点击窗口/选项卡被加载但保持隐藏。
I tried this:
我试过这个:
<script>
function OpenAndFocusHelp() {
win=window.open('help/1000CH00017.htm','help');
win.focus();
}
</script>
<a href="help.html" target="help"
onclick="OpenAndFocusHelp()">Help</a>
It did not work!
这没用!
采纳答案by bevacqua
I think this feature is browser-specific and you can't define the behavior for focusing new tabs or windows..
我认为此功能是特定于浏览器的,您无法定义聚焦新选项卡或窗口的行为。
回答by Kirk Woll
It seems that modern browsers do not allow you to window.focusan existing window. Or at least, it will not give that window focus. (IE9 will actually blink the tab, but most other browsers merely load it but give no indication that the user's attention should be drawn to the new window.)
现代浏览器似乎不允许您访问window.focus现有窗口。或者至少,它不会给那个窗口焦点。(IE9 实际上会闪烁选项卡,但大多数其他浏览器只会加载它,但不会指示应将用户的注意力吸引到新窗口上。)
Therefore, one solution that presents itself is to close the window first, and then re-open it immediately after. For example, declare the following function:
因此,出现的一种解决方案是先关闭窗口,然后立即重新打开它。例如,声明以下函数:
window.openOrFocus = function(url, name) {
if (!window.popups)
window.popups = {};
if (window.popups[name])
window.popups[name].close();
window.popups[name] = window.open(url, name);
}
Now, you can write HTML such as this:
现在,您可以编写这样的 HTML:
<a href="javascript:void(0);" onclick="openOrFocus('http://jsfiddle.net/k3t6x/2/', 'window1')">Window 1</a><br />
<a href="javascript:void(0);" onclick="openOrFocus('http://jsfiddle.net/KR6w3/1/', 'window2')">Window 2</a><br />
Since it closes the window first, it reliably gives focus to the newly created child window.
因为它首先关闭窗口,所以它可靠地将焦点放在新创建的子窗口上。
This solution also uses the window.popupsnamespace, so rename the usages of that in the javascript sample if you have a function named popupsor have otherwise collided with it.
此解决方案也使用window.popups命名空间,因此如果您有一个命名的函数popups或以其他方式与它发生冲突,请重命名 javascript 示例中该命名空间的用法。
Caveat: This does not work after a post-back. This is because once you navigate away from the current page, it is no longer the owner of the child windows. Therefore, it can no longer close them. However, it merely degrades to the usual (non-focusing) behavior of using the targetattribute.
警告:这在回传后不起作用。这是因为一旦您离开当前页面,它就不再是子窗口的所有者。因此,它不能再关闭它们。然而,它只是降级为使用该target属性的通常(非聚焦)行为。
Tested In:Firefox 4, Chrome 11, IE 9
JsFiddle Demo:http://jsfiddle.net/aqxBy/7/
测试:Firefox 4、Chrome 11、IE 9
JsFiddle 演示:http : //jsfiddle.net/aqxBy/7/
回答by Shadow Wizard is Ear For You
You can have such code instead:
您可以使用这样的代码:
var _arrAllWindows = new Array();
function OpenOrFocus(oLink, sTarget) {
var oWindow = _arrAllWindows[sTarget];
if (!oWindow || oWindow.closed) {
oWindow = window.open(oLink.href, sTarget);
_arrAllWindows[sTarget] = oWindow;
}
oWindow.focus();
return false;
}
Then to call it, have such link:
然后调用它,有这样的链接:
<a href="http://www.google.com" onclick="return OpenOrFocus(this, 'help');">Open</a>
Works fine in Chrome and IE, unfortunately Firefox disable by default the option to "raise" windows in code so focus()has no effect in that browser - could not find any work around.
在 Chrome 和 IE 中运行良好,不幸的是,Firefox 默认禁用在代码中“提升”窗口的选项,因此focus()在该浏览器中没有效果 - 找不到任何解决方法。
Test case is available here: http://jsfiddle.net/yahavbr/eVxJX/
测试用例可在此处获得:http: //jsfiddle.net/yahavbr/eVxJX/
回答by djule5
Here's one way using jQuery and HTML5 with fallback for JavaScript-disabled clients. It will reload pages on every click. Tested and works in Firefox and Chrome.
这是使用 jQuery 和 HTML5 并为禁用 JavaScript 的客户端回退的一种方法。它会在每次点击时重新加载页面。在 Firefox 和 Chrome 中测试和工作。
<script>
$(document).ready(function() {
$('a[data-popup]').click(function(event) {
event.preventDefault();
window.open(this.href, this.dataset['popup'], 'resizable,scrollbars').focus();
});
});
</script>
<a href="https://stackoverflow.com/" target="_blank" data-popup="stackoverflow">Stack Overflow</a>
<a href="https://superuser.com/" target="_blank" data-popup="superuser">Super User</a>
<a href="https://serverfault.com/" target="_blank" data-popup="serverfault">Server Fault</a>

