javascript - Window.open 打开空白的新标签页?

标签 javascript jquery html

最奇怪的事情正在发生。此单击事件打开一个空白的新选项卡,然后在原始选项卡中导航到正确的页面。这是怎么回事,我该如何解决?

我在检查器中检查了 target="_blank"属性,该属性可能是后来添加的,但什么也没有。我在我的代码中搜索了它,但什么也没有。因此 window.open 上的 _blank 属性将打开一个空白的新选项卡并导航到同一选项卡中的 url 是没有意义的。

谢谢。

$(".homedepot, .amazon, .walmart").on('click', function() {
                url = $(this).attr('href');
                window.open(url , "_blank");
                trackOutboundLink(url);
            });

    //var path = window.location.path;
                        var trackOutboundLink = function(url) {
                            var loc = function getLocation(url) {
                                var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
                                 return match && {
                                 href: href,
                                 protocol: match[1],
                                 host: match[2],
                                 hostname: match[3],
                                 port: match[4],
                                 pathname: match[5],
                                 search: match[6],
                                 hash: match[7]
                                }
                            }



                            newURL = loc.protocol + "//" + loc.host;
                            console.log(newURL)


                            ga('send', 'event', 'outbound click', 'click', newURL, {
                                'transport': 'beacon',
                                'hitCallback': function(){console.log("");}
                            });
                        }

标记:

<a class="ilHide" href="<%= product.data["product.buy_amazon"].value[0].text %>"><img class="amazon" style="width:200px; position: relative; right: .5rem;" src="images/amazon2.jpeg" alt="Amazon" /></a>
<a class="ilHide" href="<%= product.data["product.buy_homedepot"].value[0].text %>"><img class="homedepot" style="display:inline;position:relative;" src="images/homedepot.png" alt="Home Depot" /></a>
<a class="ilHide" href="<%= product.data["product.buy_walmart"].value[0].text %>"><img class="walmart" style="width:200px;" src="images/walmart.jpeg" alt="Walmart" /></a>

最佳答案

我假设您想导航到新选项卡中的 anchor href,同时留在当前页面中。如果正确,您需要:

新的点击处理程序:

$(".homedepot, .amazon, .walmart").on('click', function(e) {
    e.preventDefault();
    url = $(this).parent().attr('href');
    window.open(url , "_blank");
    trackOutboundLink(url);
});

关于javascript - Window.open 打开空白的新标签页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43727433/

相关文章:

javascript - JavaScript 中的 xml 到 json

javascript - D3 Y 轴上的旭日面包屑

asp.net - 我如何强制 ASP :TextBox to be of type email?

css - Div 不创建高度

javascript - Div 在点击时消失并显示其他 div

javascript - 使用 Clojurescript 中的 Bootstrap 3

Money 的 jQuery 自定义验证查询

jQuery:重置除当前单击的 div 之外的所有其他 div 的属性

javascript - 使用jquery增加和减少多行中的输入表单值

javascript - 如何仅为特定元素启用 document.designMode = 'on'