Safari 不让函数在页面卸载之前完成?

发布于 2024-12-04 11:03:56 字数 1261 浏览 4 评论 0原文

function log(name, value)
{
    mylog.push({'name':name, 'value':value});
}

function flush()
{   
    for (i = 0 ; i < mylog.length ; i++){
        url_str += '&n[]=' + mylog[i].name + '&v[]=' + mylog[i].value;
    }
    url_str += '&r='+Math.floor(Math.random() * 100000);

    var img = new Image();
    var url = 'http://somedomain.com/pixel.php?' + url_str;
    img.src = url;
}

if (window.attachEvent) 
    window.attachEvent('onunload', flush);
else if (window.addEventListener) 
    window.addEventListener('unload', flush, false);

我正在编写一些代码来跟踪用户交互以查找页面上的热点。其中大部分已经完成并且适用于大多数浏览器,但我目前在 Safari 5.0.5/Mac 10.6 上遇到了困难(顺便说一句,它也可以在 Windows XP 上使用 Safari 5.0.1 复制)。给我带来困难的部分是页面传输期间的链接点击。例如,下面的代码将我带到 someotherpage.html:

<form onclick="log('LINK_CLICKED', 'some other page'); this.submit();" action="someotherpage.html">Some other page</form>

该网站在所有浏览器中将我带到 someotherpage.html,但只有 Safari 5.0.5/Mac 10.6 不会在日志中显示 LINK_CLICKED。在 Safari 检查器中进行一些控制台检查后,我发现 Safari 出现“无法加载资源”错误。我怀疑这是因为虽然可以调用 URL,但浏览器已决定继续前进(?)。

无论如何,我已经尝试过 beforeunload 事件并使用 setTimeout 来延迟页面卸载。我是 考虑完全更改 HTML 表单,但这将是一项相当大的工作。不过,我对此持开放态度,除非有人的想法没有引起我的注意。关于它如何在 Safari 5.0.5 (Mac 10.6) 上工作有什么建议吗?

function log(name, value)
{
    mylog.push({'name':name, 'value':value});
}

function flush()
{   
    for (i = 0 ; i < mylog.length ; i++){
        url_str += '&n[]=' + mylog[i].name + '&v[]=' + mylog[i].value;
    }
    url_str += '&r='+Math.floor(Math.random() * 100000);

    var img = new Image();
    var url = 'http://somedomain.com/pixel.php?' + url_str;
    img.src = url;
}

if (window.attachEvent) 
    window.attachEvent('onunload', flush);
else if (window.addEventListener) 
    window.addEventListener('unload', flush, false);

I'm working on some code that tracks user interactions to find hotspots on a page. Most of it is done already and works on most browsers, but I'm currently stumped on Safari 5.0.5/Mac 10.6 (BTW, it can also be replicated with Safari 5.0.1 on Windows XP). The part that's giving me difficulty is link clicks during page transfers. For example, the code below takes me to someotherpage.html:

<form onclick="log('LINK_CLICKED', 'some other page'); this.submit();" action="someotherpage.html">Some other page</form>

The site takes me to someotherpage.html in all browsers, but only Safari 5.0.5/Mac 10.6 doesn't show a LINK_CLICKED in the logs. After doing some console checking in the Safari inspector, I found that that Safari had a "failed to load resource" error. I suspect this is because while the URL could be called, the browser had decided to move on(?).

In any case, I've tried the beforeunload event and using setTimeout to delay the page unload. I'm
considering changing the HTML form altogether, but it's going to be quite an effort. Still, I'm open to it unless someone has ideas that has skipped my attention. Any suggestions on how it could work on Safari 5.0.5 (Mac 10.6)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-12-11 11:03:56

我认为您只是遇到了竞争状况,可以通过跟踪像素负载的回调来缓解这种状况。

function log(name, value, callback)
{
    mylog.push({'name':name, 'value':value, 'callback':callback});
}


function flush()
{   
    for (i = 0 ; i < mylog.length ; i++){
        url_str += '&n[]=' + mylog[i].name + '&v[]=' + mylog[i].value;
    }
    url_str += '&r='+Math.floor(Math.random() * 100000);

    var img = new Image();
    img.onload = logReady;
    var url = 'http://somedomain.com/pixel.php?' + url_str;
    img.src = url;
}

function logReady()
{
  for (i = 0 ; i < mylog.length ; i++){
       mylog[i].callback();
    }
}



<form onclick="log('LINK_CLICKED', 'some other page', function() { form.submit(); });" action="someotherpage.html">Some other page</form>

I think you are just running into a race condition that could be alleviated with a callback on the tacking pixel load.

function log(name, value, callback)
{
    mylog.push({'name':name, 'value':value, 'callback':callback});
}


function flush()
{   
    for (i = 0 ; i < mylog.length ; i++){
        url_str += '&n[]=' + mylog[i].name + '&v[]=' + mylog[i].value;
    }
    url_str += '&r='+Math.floor(Math.random() * 100000);

    var img = new Image();
    img.onload = logReady;
    var url = 'http://somedomain.com/pixel.php?' + url_str;
    img.src = url;
}

function logReady()
{
  for (i = 0 ; i < mylog.length ; i++){
       mylog[i].callback();
    }
}



<form onclick="log('LINK_CLICKED', 'some other page', function() { form.submit(); });" action="someotherpage.html">Some other page</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文