在异步Javascript加载之前页面重定向

时间:2014-07-29 06:36:01

标签: javascript html redirect asynchronous mixpanel

我正在尝试制作一个重定向页面,用mixpanel.track跟踪该页面的加载

问题是,在异步javascript有时间下载和运行之前,页面会重定向。我该怎么办?

Here是我的代码。

编辑:我将代码放在问题中。

<!DOCTYPE HTML>
<html lang="en-US">
<head>

<!-- start Mixpanel --><script type="text/javascript">(function(f,b){if(!b.__SV){var a,e,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user".split(" ");
for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=f.createElement("script");a.type="text/javascript";a.async=!0;a.src="//cdn.mxpnl.com/libs/mixpanel-2.2.min.js";e=f.getElementsByTagName("script")[0];e.parentNode.insertBefore(a,e)}})(document,window.mixpanel||[]);
mixpanel.init("9f85e6edf009562d5ac2c944b0da6398");mixpanel.track("CBhopID_x3cw17x1h")</script><!-- end Mixpanel -->

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="1;url=http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/" http-equiv="refresh" />

<script type="text/javascript">
window.location.href = "http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/"
</script>

<title>Redirecting...</title>
</head>
<body>
<p>If you are not redirected automatically, follow <a href="http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/">this link.</a></p>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

mixpanel.track()有回调选项,会在跟踪事件后调用,您需要使用它,在回调时您可以重定向到某个位置,将您的window.location代码放在回调函数中,如:< / p>

mixpanel.track("CBhopID_x3cw17x1h", {}, function() {
   window.location.href = "http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/";
});