使用脚本设置co​​okie的最佳方法然后重定向?

时间:2013-09-24 20:36:56

标签: php cookies url-redirection

我需要在重定向到另一个域之前设置第三方跟踪Cookie。

第三方提供了此脚本。

<script type='text/javascript'>
    var ssa = '8g38f7g5yg8H84hh5';
    var ssaUrl = ('https:' == document.location.protocol ? 'https://' : 'http://') +      'pixel.trackingdomain.com/iap/' + ssa;
    new Image().src=ssaUrl;
</script>

最好/最快的方法是什么?

2 个答案:

答案 0 :(得分:0)

由于您需要等到跟踪请求完成,请使用onload事件来推迟重定向:

var ssa = '8g38f7g5yg8H84hh5';
var ssaUrl = ('https:' == document.location.protocol ? 'https://' : 'http://') +
              'pixel.trackingdomain.com/iap/' + ssa;
var img = new Image();
img.onload = function() {
    document.location.href = "http://redirect.to.somewhere/";
};
img.src=ssaUrl;

答案 1 :(得分:0)

如果您使用的是jQuery,那么您可以使用下面提到的代码。

var ssa = "8g38f7g5yg8H84hh5";
var ssaUrl = window.location.protocol + "//" + window.location.host + "/pixel.trackingdomain.com/iap/" + ssa;
var tracker = $("<img/>").attr({ src: ssaUrl }).bind("load", function (event) {
        window.location.href = "http://redirect.to.somewhere/";
    }).appendTo("body");
相关问题