如何告诉网页浏览器用户修改了页面? [英] How do I tell the web browser that the user has modified the page?

查看:112
本文介绍了如何告诉网页浏览器用户修改了页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户在html页面上进行一些可视化设置。可能需要一些时间,所以我不希望用户以某种方式关闭网页(没有警告)来完成安装。

I let the user do some visual setup on a html page. It may take some time so I do not want the user to loose the setup done by closing the web page in some way (without a warning).

如果我使用文本区域,复选框等,浏览器将会向用户发出警告。

If I had used text areas, check boxes etc then the browser would warn the user. How do I tell the browser to warn the user in my case?

推荐答案

您可以使用全局变量,如unsavedChanges下面的例子。那么如果用户更改任何内容,则需要设置该变量。或者在用户关闭网页之前想要确认的任何情况下。

You can use a global variable like "unsavedChanges" in below example. Then you need to set that variable if user change anything. Or in any condition when you want that confirmation before user close the web page.

 var unsavedChanges = 0;
 window.onbeforeunload = function (e) {
    if(unsavedChanges != 0){
         e = e || window.event;

         // For IE and Firefox prior to version 4
         if (e) {
            e.returnValue = 'There is some unsaved changes!';
         }

         // For Safari
         return 'There is some unsaved changes!';
    }
};

这篇关于如何告诉网页浏览器用户修改了页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆