使用 window.location 在 JavaScript 函数中_blank窗口

Using window.location to _blank window in JavaScript function

本文关键字:blank 窗口 函数 JavaScript window location 使用      更新时间:2023-09-26

我正在努力打开一个新窗口,其中包含基于链接的重定向和点击功能。有什么想法我哪里出错了吗?

该功能的原因是掩盖我们的会员网址,通过发送Google Analytics来跟踪点击,并将用户带到带有会员链接的新页面。

目前发生的事情是,当点击时,点击被跟踪,但随后在同一窗口和新窗口中加载会员链接。

JS函数;

 $(function()
        {
            /**
             * Affiliate Link Click Listener
             * ---
             * 1. prevent the default behaviour.
             * 2. Grab the affiliate url.
             * 3. run google tracking code.
             * 4. redirect the user.
             **/
            $('.affiliate-link').on('click', function(e){
                // Prevent default behaviour
                e.preventDefault();
                // Get the affiliate link
                var affiliate_link = $(this).attr('data-link');
                console.log(affiliate_link);
                // Call our google tracking function
                trackOutboundLink(affiliate_link);
                // Do the redirect
                window.open(affiliate_link, '_blank');
            });
        });
/**
     * Function that tracks a click on an outbound link in Analytics
     * This function takes a valid URL string as an argument, and uses that URL string
     * as the event label. Setting the transport method to 'beacon' lets the hit be sent
     * using 'navigator.sendBeacon' in browser that support it.
     */
    var trackOutboundLink = function(url) {
        ga('send', 'event', 'outbound', 'click', url, {
            'transport': 'beacon',
            'hitCallback': function(){document.location = url;}
        });
    }

链接;

<a class="apply-btn affiliate-link" href="" data-link="<?= $product['tracking_link']?>" target="_blank">SEE DEAL &raquo;</a>

我最初window.location.href = affiliate_link;进行重定向,但一些搜索显示无法定位空白窗口。

 $(function()
        {
            /**
             * Affiliate Link Click Listener
             * ---
             * 1. prevent the default behaviour.
             * 2. Grab the affiliate url.
             * 3. run google tracking code.
             * 4. redirect the user.
             **/
            $('.affiliate-link').on('click', function(e){
                // Prevent default behaviour
                e.preventDefault();
                // Get the affiliate link
                var affiliate_link = $(this).attr('data-link');
                console.log(affiliate_link);
                // Call our google tracking function
               // trackOutboundLink(affiliate_link);
                // Do the redirect
                window.open(affiliate_link, '_blank');
            });
        });

此方法中的错误 trackOutboundLink(affiliate_link); 当我注释此行时,您的代码运行