blank window in JavaScript function
I'm struggling to open a new window with a redirect based on and onclick function of a link. Any ideas where I am going wrong?
The reason for the function is to mask our affiliate urls, track the click by sending Google Analytics and take the user to a new page with the affiliate link.
At present what is happening is when clicked the click is tracked but then the affiliate link loads in same window and a new window.
JS Function;
$(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;}
});
}
Link;
<a class="apply-btn affiliate-link" href="" data-link="<?= $product['tracking_link']?>" target="_blank">SEE DEAL »</a>
I originally had window.location.href = affiliate_link;
to do the redirect but a few searches revealed it wasn't possible to target a blank window.
$(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');
});
});
//Error in this method trackOutboundLink(affiliate_link); trackOutboundLink is undefiend when me comment this line your code run
链接地址: http://www.djcxy.com/p/12428.html上一篇: jQuery和AJAX响应头
下一篇: JavaScript函数中的空白窗口