简体   繁体   English

html打开新目标和焦点的网址

[英]html open a url on new target and focus

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! 那没起效!

It seems that modern browsers do not allow you to window.focus an existing window. 现代浏览器似乎不允许你对现有窗口进行窗口window.focus 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.) (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.popups namespace, so rename the usages of that in the javascript sample if you have a function named popups or 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 target attribute. 但是,它只会降低到使用target属性的通常(非聚焦)行为。

Tested In: Firefox 4, Chrome 11, IE 9 测试版: Firefox 4,Chrome 11,IE 9
JsFiddle Demo: http://jsfiddle.net/aqxBy/7/ JsFiddle演示: http //jsfiddle.net/aqxBy/7/

我认为此功能是特定于浏览器的,您无法定义聚焦新选项卡或窗口的行为。

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/

Here's one way using jQuery and HTML5 with fallback for JavaScript-disabled clients. 这是使用jQuery和HTML5的一种方法,用于禁用JavaScript的客户端的后备。 It will reload pages on every click. 它会在每次点击时重新加载页面。 Tested and works in Firefox and Chrome. 经过测试,适用于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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM