简体   繁体   English

过多的javascript代码

[英]Too much redundant javascript code

On my asp.net pages i need to control for Dirty controls. 在我的asp.net页面上,我需要控制Dirty控件。 I'm not that good in javascript so i found a solution on the internet for doing that by doing so : 我的JavaScript不太好,所以我在网上找到了一个解决方案,方法是:

<script type="text/javascript" language="javascript">
    //Demo of completely client side dirty flag.

    function setDirty() {
        document.body.onbeforeunload = showMessage;
        //debugger;
        document.getElementById("DirtyLabel").className = "show";
    }

    function clearDirty() {
        document.body.onbeforeunload = "";
        document.getElementById("DirtyLabel").className = "hide";
    }


    function showMessage() {
        return ""
    }

    function setControlChange() {
        if (typeof (event.srcElement) != 'undefined') {
            event.srcElement.onchange = setDirty;
        }
    }

    document.body.onclick = setControlChange;
    document.body.onkeyup = setControlChange;

    window.onunload = function (sender, eventArgs) {
        if (window.opener != null) {
            window.opener.ClearBlocker();

            if (window.opener.TempClientReturnFunction != null)
                window.opener.TempClientReturnFunction = window.opener.ReturnFunction;
        }
    }
</script>

but if i have like 7 pages where i need to control for Dirty Controls, its gonna be too much redundant code. 但是,如果我有7页需要控制脏页控制,那么它将会是过多的冗余代码。 Are there any ways to create some class/library from where i can just call functions or are there maybe even smarter way of doing that? 有什么方法可以从中创建一些类/库,而我只能从那里调用函数,或者有更聪明的方法呢?

As already suggested, put your code into a separate file, then include on every page that needs it. 正如已经建议的那样,将您的代码放入一个单独的文件中,然后在需要它的每个页面上进行添加。

Something else you might like to consider is to adopt the "module" pattern, thereby putting no more than one member into the javascript global namespace. 您可能要考虑的其他事情是采用“模块”模式,从而将不超过一个成员放入javascript全局命名空间。

Modulized, your code will look something like this: 模块化后,您的代码将如下所示:

var DIRTY_CONTROL = (function(){ //module pattern
    // *** Private members ***
    var dirtyLabelId = "DirtyLabel";
    var showMessage = function() {
        return "";
    };
    var setDirty = function(id) {
        id = id || dirtyLabelId;
        document.body.onbeforeunload = showMessage;
        //debugger;
        document.getElementById(id).className = "show";
    }
    var clearDirty = function clearDirty(id) {
        id = id || dirtyLabelId;
        document.body.onbeforeunload = "";
        document.getElementById(id).className = "hide";
    };
    var setControlChange = function(e) {
        e = e || window.event;
        var target = e.target || e.srcElement;
        target.onchange = setDirty;
    };
    var init = function() {
        document.body.onclick = document.body.onkeyup = setControlChange;
        window.onunload = function() {
            if (window.opener) {
                if (window.opener.ClearBlocker) {
                    window.opener.ClearBlocker();
                }
                if (window.opener.ReturnFunction) {
                    window.opener.TempClientReturnFunction = window.opener.ReturnFunction;
                }
            }
        };
    };
    // *** Public members ***
    //Expose private members here, as required.
    return {
        clearDirty: clearDirty,
        setDirty: setDirty,
        init: init
    };
})();

untested 未经测试

By convention, the module's name is capitalized. 按照惯例,模块名称大写。

Call public functions as follows: 如下调用公共函数:

DIRTY_CONTROL.init();

or 要么

DIRTY_CONTROL.clearDirty();

Put that JS block into a JS file like Dirty.js, then use the following code on every page you want to use it on. 将该JS块放入Dirty.js之类的JS文件中,然后在要使用它的每个页面上使用以下代码。

<script src="Dirty.js"></script>

You can add it between the tag, or at the end of the body depending on when you need to actually call it. 您可以将其添加到标签之间,也可以添加到正文的末尾,具体取决于您何时需要实际调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM