我可以让浏览器专注于这些链接吗?

Can I get browsers to focus on these links

本文关键字:链接 于这些 专注 浏览器 我可以      更新时间:2023-09-26

Fiddo

这些链接将在新选项卡中打开,但如果您返回到小提琴并单击另一个链接,浏览器不会将您的视图切换到该选项卡。它将"酷"选项卡重新加载到另一个链接,但我希望浏览器专注于新选项卡。

<a href="http://www.google.com/" target="cool">google</a>
<a href="http://www.yahoo.com" target="cool">yahoo</a>

为了引用另一个窗口,您需要将其引用保存在变量中。 获得该引用后,可以使用window对象函数对其进行操作。 例如:

<!DOCTYPE html>
<html>
<head>
    <title>Focus tab test</title>
    <meta charset="utf-8" />
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script>
        $(document).ready(function() {
            var cool;
            $("a").click(function(){
                event.preventDefault();
                if (typeof cool === "undefined") {
                    cool = window.open(this.href, "cool");
                } else {
                    cool.close();
                    cool = window.open(this.href, "cool");
                }
            });
        });
    </script>
</head>
<body>
<a href="http://www.microsoft.com/">microsoft</a>
<a href="http://www.yahoo.com">yahoo</a></body>
</html>

请注意,我不能将其设为 jsFiddle,因为它们会阻止您在它们的框架中打开选项卡。 另请注意,我必须销毁旧窗口并打开一个新窗口,而不是更新 location.href 属性并设置其焦点。 这是因为JS无法访问另一个窗口的属性,除非协议(http),主机名和端口都匹配。