Page is Dirty using JavaScript

Dirty Means

If user change any data or check any box or select any value from drop down and he forget to save the data and click on exit button or closing the browser then if you want to show the message to user, before cancel or exit please save the data.

Here is the code.

<script type="text/javascript" language="javascript">

    function confirmExit(isHeaderCheckRequired) {

        //debugger;

        var ask = 'You have done some changes which are not saved yet.\n\n'

+ 'You will loose unsaved changes if you proceed!\n'

+ 'OK -> Loose changes Cancel -> Stay\n\n'

+ 'Press OK to continue, or Cancel to stay on the current page.';

        var result = true, isModified = false;

        var hfModified = null;

 

        hfModified = $get('<%= hfRMPModified.ClientID %>');

        pnlRisk = $get('<%=pnlRiskActionPlan.ClientID %>');

        pnlRiskHeaderDetails = $get('<%=pnlRMPHeaderData.ClientID %>');

 

        //check if modified, if yes then confirm, if yes then set the active tab index

        if (hfModified.value == '1') {

            isModified = true;

        }

        else {

            if (pnlRisk != null && pnlRiskHeaderDetails != null) {// panel) {

                isModified = is_dirty(pnlRisk, pnlRiskHeaderDetails, isHeaderCheckRequired);

            }

        }

        if (isModified == true) {

            result = confirm(ask);

            if (result == false) { }

            else { hfModified.value = '0'; }

        }

        return result;

    }

    function is_dirty(pnlRisk, pnlRiskHeaderDetails, isHeaderCheckRequired) {

        var result = false;

        if (isHeaderCheckRequired)

            result = isPanelModified(pnlRiskHeaderDetails);

        if (!result && pnlRisk.style.display != 'none') {

            result = isPanelModified(pnlRisk)

        }

        return result;

    }

    function isPanelModified(panel) {

        var tage = panel.getElementsByTagName("input");

        for (var i = 0; i < tage.length; i++) {

            switch (tage[i].type) {

                case 'text':

                case 'textarea':

                    if (!/^\s*$/.test(tage[i].value) && tage[i].value != tage[i].defaultValue) {

                        if (tage[i].value != '__/___/____') return true;

                    }

                    break;

                case 'checkbox':

                case 'radio':

                    if (tage[i].checked != tage[i].defaultChecked) return true;

                    break;

            }

        }

        var tage = panel.getElementsByTagName("TEXTAREA");

        for (var i = 0; i < tage.length; i++) {

            if (!/^\s*$/.test(tage[i].value) && tage[i].value != tage[i].defaultValue) {

                return true;

            }

        }

        tage = panel.getElementsByTagName("select")

        for (var i = 0; i < tage.length; i++) {

            if (tage[i].id == 'ctl00_CPHSiteMaster_ftbRiskDescription_0_0' || tage[i].id == 'ctl00_CPHSiteMaster_txtActionItem_0_0')

                continue;

            else {

                switch (tage[i].type) {

                    case 'select-one':

                    case 'select-multiple':

                        var el, j = 0;

                        while (el = tage[i].options[j++])

                            if (el.selected != el.defaultSelected)

                                return true;

                        break;

                }

            }

        }

        return false;

    }

</script>

Ebook Download
View all
Learn
View all