使用javascript缓存问题

时间:2013-08-13 02:49:50

标签: javascript caching cross-browser

我有这个javascript代码来检测页面上的字段更改。它与IE运行良好,但不幸的是没有使用Chrome。我真的不知道为什么会这样。当我尝试通过不同的浏览器和单位访问它时。这个JavaScript运行不正常。我需要清除缓存历史记录才能使其正常工作。这是代码:

var ctr = 1;
    var isFirstLoad = true;


    function propertyChangedByCode() {
        ctr = 0;
        propertyChanged();
    }

    function propertyChanged() {
        if (document.readyState == "complete") {
            $('[id$=SaveButton]').removeAttr("disabled");
            $('[id$=SaveButton]').removeAttr("class");
            $('[id$=SaveButton]').addClass("btn btn-primary");
            warnMessage = 'Warning: You have unsaved changes.';

            console.log(ctr);

            window.onbeforeunload = function () {
                if (ctr == 0) {
                    return warnMessage
                }
                else if (ctr == 1) {
                    ctr = 0;
                }
            }
        }
    }

    $("body").click(function (event) {
        console.log(event.target);
        if (event.target.type == "submit" && event.target.value != "Save" && event.target.value != "Cancel" && event.target.value != "Next") {
            ctr = 0;
            propertyChanged();
            console.log("BODY clicked");
        }
    });

    function pageLoad() {

        jQuery("input.datepicker").dp({
            dateFormat: 'M d, yy',
            altFormat: 'yy-mm-dd',
            buttonText: '<i class="icon icon-calendar"></i>',
            showOn: 'both'
        });

        $("button.ui-datepicker-trigger").addClass('btn right');

        console.log("PageOnLoad: " + ctr);

        var warnMessage = 'Warning: You have unsaved changes.';
        $('input[type="text"],select,textarea').each(function (k, v) {

            var elem = $(this);
            // Save current value of element
            elem.data('oldVal', elem.val());

            // Look for changes in the value
            elem.bind("change keyup input paste", function (event) {
                ctr = 0;
                // If value has changed...
                if (elem.data('oldVal') != elem.val()) {
                    propertyChanged();
                }
            });
        });

        $('.btn').click(function (e) {
            console.log("whatever");
            ctr = 1;
        });

        //ui-datepicker-trigger
        $('.ui-datepicker-trigger').click(function (e) {
            console.log("date");
            ctr = 0;
        });

        if (isFirstLoad) {
            window.onbeforeunload = null;
            $('[id$=SaveButton]').attr("disabled", "disabled")

            console.log("Disable");

            isFirstLoad = false;
        }

因此,我决定采用禁用系统缓存的路径。我把这段代码放在我的主页page_load上。

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
Response.Cache.SetNoStore()
Response.Cache.SetAllowResponseInBrowserHistory(True)

根据我的研究,这三行将禁用我的应用程序中的缓存。试图清除我的缓存,在我的字段上输入一些值,点击保存,然后再次浏览到该页面,但仍然是字段在自动填充中显示我之前的输入。所以我假设禁用缓存不起作用。你有什么想法为什么禁用缓存不起作用?您对浏览缓存有什么建议吗?为什么我认为我遇到了清除缓存所需的问题,以使我的javascript正常工作?最后,为什么它在我的IE上工作但不在Chrome中呢?谢谢!很抱歉问题很多。

0 个答案:

没有答案