jQueryUI Dialog + Firefox + ASP.Net = access to strict mode caller function is censored

浪尽此生 提交于 2019-11-30 17:13:47
Pedro Fonseca

This is kind of an old post, but this issue still occurred to me today. I didn't wanted to use the click of the button, so instead i tried a setTimeout and it also works.

To people having this issue, try this solution:

setTimeout(function() { __doPostBack('DateButton', $('#txtDate').val()); }, 1);

Having recently had to address this issue I found that I could solve the problem and get __doPostBack working in Edge, IE, Chrome & FireFox by adding the following script to the top of my app.

    if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { window.event = {}; }    

Using the SetTimeout function breaks Edge. Just putting in window.event={} broke IE.

While this doesn't actually explain how to fix the problem, this is a working work-around.

I created a hidden field that corresponded to each field in the dialog box. Then I created a button. These had to be kept outside of the dialog box div because the div is moved to outside the form when it is turned in to a dialog box. Then I modified my dialog box creation code to be something like this:

$('#date-dialog').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    buttons: {
        "Submit": function () {
            $('#<%=hfDate.ClientID %>').val($('#txtDate').val());
            $('#<%=btnFormSubmit.ClientID %>').click();
            $(this).dialog("close");
        },
        "Cancel": function () {
            $(this).dialog("close");
        }
    }
});

This is working for IE, Chrome and Firefox. browser!, Thanks to https://mnaoumov.wordpress.com/2016/02/12/wtf-microsoftajax-js-vs-use-strict-vs-firefox-vs-ie/

function hackEventWithinDoPostBack() {
var originalEventDescriptor = Object.getOwnPropertyDescriptor(Window.prototype, "event");
    var hackEventVariable = false;
    var eventPropertyHolder;
    Object.defineProperty(window, "event", {
        configurable: true,
        get: function get() {
            var result = originalEventDescriptor ? originalEventDescriptor.get.apply(this, arguments) : eventPropertyHolder;
            if (result || !hackEventVariable)
                return result;
            return {};
        },
        set: function set(value) {
            if (originalEventDescriptor)
                originalEventDescriptor.set.apply(this, arguments);
            else
                eventPropertyHolder = value;
        }
    });
    var originalDoPostBack = window.__doPostBack;

    window.__doPostBack = function hackedDoPostBack() {
        hackEventVariable = true;
        originalDoPostBack.apply(this, arguments);
        hackEventVariable = false;
    };
}

hackEventWithinDoPostBack();

This happened to me in Firefox Quantum. I was using a jQuery 3.x CDN, I had to download it to reference it locally in my project. After that I edited it (jquery.3.x.min.js) and commented/removed the following:

"use strict";

I had to do a search to remove them all. There were two.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!