简体   繁体   English

单击我网站上的链接时打开新标签页,保持当前标签页处于活动状态

[英]Opening new tab when clicking on a link in my website keeping the current tab active

I have this code that does what I need. 我有这段代码可以满足我的需求。 It opens the link in another tab but it moves to the newly opened tab leaving the current tab I'm in. I want it to keep my current tab active and newly opened tab just opened. 它会在另一个选项卡中打开链接,但会移动到新打开的选项卡,而使我停留在当前选项卡中。我希望它保持当前选项卡处于活动状态,而新打开的选项卡才刚刚打开。

function loadpopunder() {
                var clicked = false;
                $('a').each(function() {
                    this.onclick = function() {
                        var a = document.createElement("a");
                        a.href = "http://sitetopopeninnewtab.com";
                        if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
                        {
                            a.target = "_blank";
                        }
                        if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
                        {
//                        var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, });
                            var evt = document.createEvent("MouseEvents");
                        }
                        else {
//                        var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, 'metaKey': true, 'ctrlKey': true});
                            var evt = document.createEvent("MouseEvents");
                        }
                        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                true, false, false, false, 0, null);
                        if (clicked == false) {
                            a.dispatchEvent(evt);
                            clicked = true;
                        }
                    }
                });


if(window.location.href == 'http://mymainwebsite.com/'){
    loadpopunder();
}

This can't be done properly, because you depend on the browser settings. 无法正确完成此操作,因为您取决于浏览器设置。 Even when you open a url in a new tab with target='_blank' , if the browser is set to open new pages in a pop-up window, you wont get a new tab. 即使使用target='_blank'在新标签页中打开url,如果将浏览器设置为在弹出窗口中打开新页面,也不会获得新标签页。

You should check some other SO answer about it here and here . 您应该在这里这里检查有关问题的其他一些答案。

You can see on the comments that all browser have different behaviors. 您可以在评论中看到所有浏览器都有不同的行为。 You can, however, manage new opened windows with javascript window object 但是,您可以使用javascript window对象管理新打开的窗口

Other thing that you can do, is check if the window is focused or not, by using the javascript visibility api . 您可以做的另一件事是使用javascript 可见性api检查窗口是否聚焦。

Hope that helps. 希望能有所帮助。

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

相关问题 弹出窗口来了,当我的 WordPress 网站中的新选项卡中打开链接时,要求我重定向到外部 URL - Popup comes, ask me to redirect to external URL when the link opening in new tab in my WordPress website 我的链接没有打开它应该打开的页面,但会在新标签中打开 - My link is not opening the page it should but will in a new tab 单击链接按钮时打开一个新选项卡 - open a new tab when clicking a link button 从当前选项卡上下文中打开一个新选项卡(何时关闭弹出窗口)? - Opening a new tab from current tab context (when to close the popup)? 右键单击网站上的链接时,缺少“在新标签页中打开链接”选项 - 'open link in new tab' option missing when right clicking a link on website 单击(Ctrl +单击)链接时停止打开新标签页,而不更改href的详细信息 - Stop New Tab opening when clicking (Ctrl + Click ) on a link without href detail change 事件发生在同一选项卡中,同时在 Firefox 扩展中打开从活动选项卡到新选项卡的链接 - Event occurring in same tab, while opening a link from active tab to new tab in Firefox extension 在新标签页中打开新链接 - Opening new link in new tab 当链接在新选项卡中打开时如何保留在当前窗口选项卡上 - How to stay on current window tab when the link opens in new tab 链接未在 JStree 的新选项卡中打开 - link not opening in new tab in JStree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM