关闭浏览器时保存数据 [英] Save data while closing browser
本文介绍了关闭浏览器时保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
关闭浏览器时,我必须保存数据.我已经在页面
Hello,
I have to save data while closing the browser. I have done it on page
"onbeforeunload"
事件上完成了此操作,但仅在IE中有效.您能帮助我使用其他浏览器保存数据吗?
event but it is working only in IE. can you help me in saving data using other browser
推荐答案
好吧,我不确定您要保存数据的确切方式.但是要使事件beforeunload可以在其他浏览器上使用,请查看以下链接 [^ ].虽然它代表FF,但也适用于其他浏览器.
虽然,这是个话题,但最近我一直在爱 jQuery [
Well, I am not sure what you are exactly doing to save the data. But to make the event beforeunload to work for other browsers, have a look at this link[^]. Though, it talks for FF, but works same for others browsers as well.
Though, it is off topic but lately I have been loving jQuery [^]for its capability to handle these cross broswer implementation. With jQuery you can do something like this.
(document).ready(function () { jQuery(window).bind(''beforeunload'', function () { //save my data SaveData(); }); });
(document).ready(function () { jQuery(window).bind(''beforeunload'', function () { //save my data SaveData(); }); });
向OPs问题添加更多解释到哪里写此代码?
将此代码添加到头部.还请查看 https://developer.mozilla.org/en/DOM/window.onbeforeunload
adding more explanation to OPs question to where to write this code?
add this code in the head section. also look at https://developer.mozilla.org/en/DOM/window.onbeforeunload
<head>
<script>
window.onbeforeunload = function (e)
{
var e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
</script>
</head>
Rachit,
OnBeforeUnload似乎是他们开发的那些Microsoft IE怪癖之一.据我所知,它仅在IE和Safari中有效.但是,标准事件是OnUnload.这在其他3种浏览器中都存在; Firefox,Opera和Google Chrome.我设法在所有浏览器中使用的代码是:
Hi Rachit,
OnBeforeUnload appears to be one of those Microsoft IE quirks they have developed. It only works in IE and Safari so far as I can tell. However, the standard event is OnUnload. This owkrs in the 3 other mian browsers; Firefox, Opera and Google Chrome. The code I managed to get working in all browsers was:
<html>
<head>
<title></title>
<script type="text/javascript">
function Closing()
{
alert('Closing');
}
</script>
</head>
<body onunload="Closing();" onbeforeunload="Closing();">
</body>
</html>
因此,只需用保存数据代码替换关闭方法中的警报即可.
希望这会有所帮助,
埃德:)
So simply replace the alert in the closing method with your save data code.
Hope this helps,
Ed :)
这篇关于关闭浏览器时保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文