iPhone/iTouch 未触发(新图像)卸载请求
嘿伙计们,希望有人能帮助我。
我正在制作一个小应用程序来记录点击,这一切进展顺利,直到我使用 iPhone/iTouch。我想指出的是,我一直在使用 iTouch 进行测试,并且我只是假设 iPhone 上也会发生同样的情况。
我现在所拥有的与此类似,
<script>
function save(){
// Capture link, with var mycoords containing string
var a = 'http://mydomain.com/capture.php?co='+mycoords;
var img = new Image(1,1);
// Loads link with params, PHP uses $_GET
img.src = a;
alert('f'); // For testing
}
// alerts 'f', does not send data on device
window.onunload = save;
// alerts 'f', does not send data on device
window.onunload = (function(){
save();
});
// Alerts 'f', does not send data on device
window.addEventListener("unload",save,false);
</script>
此代码适用于我所有的桌面浏览器,包括 Safari,但在 iTouch/iPhone 上则不行。如果我在任何 onunload 实践之外执行 save() 函数,数据发送得很好。由于警报,我知道 onunload 有效。但我对图像对象在这种情况下不起作用但在其他地方工作却感到非常困惑。我在网上仔细查了一下,没有找到解决办法。
也许这里有人可以给我一个解决方案或解释为什么会发生这种情况?我将非常感激。感谢您抽出时间。
Hey guys, hope someone can help me out.
I'm making a small application to record clicks, which is going great, until I hit the iPhone/iTouch. I'd like to point out, that I've been testing with an iTouch, and I am just presuming the same thing will happen on a iPhone.
What I have at the moment is something similar to this,
<script>
function save(){
// Capture link, with var mycoords containing string
var a = 'http://mydomain.com/capture.php?co='+mycoords;
var img = new Image(1,1);
// Loads link with params, PHP uses $_GET
img.src = a;
alert('f'); // For testing
}
// alerts 'f', does not send data on device
window.onunload = save;
// alerts 'f', does not send data on device
window.onunload = (function(){
save();
});
// Alerts 'f', does not send data on device
window.addEventListener("unload",save,false);
</script>
This code works on all my desktop browsers, including Safari, but on the iTouch/iPhone, no. If I execute the save() function outside any onunload practice, the data sends just fine. I know the onunload works, due to the alerts. But I am absolutely baffled by image object not working in this instance, yet working everywhere else. I have had a good look about on the net, and have found no solution.
Perhaps someone here can maybe give me a solution or an explanation to why this is happening? I would be very grateful. Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上没有办法可靠地检测 onunload,正如这里所回答的:safari 中的 javascript unload 事件移动?
如果您尝试跟踪点击,我建议向链接添加一个点击事件处理程序,该处理程序将首先调用您的 save() 函数,然后它可以返回 true 或将 document.location.href 更新为将用户带到最终链接。
Basically there is no way to detect onunload reliably, as is answered here: javascript unload event in safari mobile?
If you're trying to track clicks, I would suggest adding a click event handler to the link which would first call your save() function and then it could return true or update the document.location.href to take the user to the final link.