ページを離れる時に資料の修正があるかどうかを確認するために、変更があれば確認ボックスをポップアップします.

2227 ワード

ヒント
//                ,            
(function () {
    var E = YAHOO.util.Event;
    var D = YAHOO.util.Dom;
    var oldContent = '';
    var newContent = '';
    var contentReady = false;
    var form = (D.getElementsByClassName("infoForm"))[0];

    var getOldContent = function () {
        oldContent = '';
        for (var i = 0; i < form.length; i++) {
            if (form.elements[i].type == 'hidden')
                continue;
            //                
            if (form.elements[i].value == "            ")
                continue;
            if (form.elements[i].type == 'radio') {
                oldContent += form.elements[i].checked;
                continue;
            }
            if (form.elements[i].type == 'checkbox') {
                oldContent += form.elements[i].checked;
                continue;
            }
            oldContent += form.elements[i].value;
        }
        contentReady = true;
    }

    var checkEdit = function (e) {
        var newContent = '';
        for (var i = 0; i < form.length; i++) {
            if (form.elements[i].type == 'hidden')
                continue;
            if (form.elements[i].value == "            ")
                continue;
            if (form.elements[i].type == 'radio') {
                newContent += form.elements[i].checked;
                continue;
            }
            if (form.elements[i].type == 'checkbox') {
                newContent += form.elements[i].checked;
                continue;
            }
            newContent += form.elements[i].value;

        }
        if (newContent != oldContent)
            e.returnValue = '======================================
' + ' , ?
' + '======================================'; } E.onDOMReady(function () { setTimeout(getOldContent, 1000); }); E.addListener(window, 'beforeunload', function (e) { if (contentReady == true) checkEdit(e); }); form.onsubmit = function () { getOldContent(); }; })();