Client/JS Framework for “Unsaved Data” Protection?

前端 未结 6 2148
一生所求
一生所求 2020-11-30 01:04

We have a typical web application that is essentially a data entry application with lots of screens some of which have some degree of complexity. We need to provide that st

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 01:25

    Additional to Lance's answer, I just spent an afternoon trying to get this snippet running. Firstly, jquery 1.4 seems to have bugs with binding the change event (as of Feb '10). jQuery 1.3 is OK. Secondly, I can't get jquery to bind the onbeforeunload/beforeunload (I suspect IE7, which I'm using). I've tried different selectors, ("body"), (window). I've tried '.bind', '.attr'. Reverting to pure js worked (I also saw a few similar posts on SO about this problem):

    $(document).ready(function() {
        $(":input").one("change", function() {
            window.onbeforeunload = function() { return 'You will lose data changes.'; }
        });
        $('.noWarn').click(function() { window.onbeforeunload = null; });
    });
    

    Note I've also used the ':input' selector rather than enumerating all the input types. Strictly overkill, but I thought it was cool :-)

提交回复
热议问题