简体   繁体   中英

Uncaught TypeError: Cannot read property 'document' of undefined

I have the following function, which works fine on a few PCs that I've tested on. I've tested this on Chrome, IE & Firefox with no issues. However, there's 1 particular PC (running Chrome), that throws this error "Uncaught TypeError: Cannot read property 'document' of undefined" on the line:

                win.document.write(data);

Could it be because win is null?

If so, why is this the case on this particular PC?

Is there some Chrome settings that needs to be set?

Method:

    function viewReport() {
        console.info('generating event report');
        var frmData = $('#frmEventReport').serializeArray();
        var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
        console.info('generated random report name ' + rptName);
        $.ajax({
            //type: "GET",
            timeout: 120000,
            url: '@Url.Action("EventReport", "Reports")',
            data: frmData,
            success: function (data) {
                console.info('succesfully called back');
                var win = window.open('', rptName, '_blank');
                console.info('opening window');
                win.document.write(data);

            },
            error: function (x, y, z) {
                console.info(x + ' ' + y + ' ' + z);
            }
        });
    }

Are popups enabled on that PC's Chrome? If they're not then the new window cannot be created hence win is undefined

This is because when you try to create a pop-up after the success of asynchronous ajax request win get undefined . So you can add async:false as follows this is works for me:

function viewReport() {
    console.info('generating event report');
    var frmData = $('#frmEventReport').serializeArray();
    var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
    console.info('generated random report name ' + rptName);
    $.ajax({
        //type: "GET",
        timeout: 120000,
        url: '@Url.Action("EventReport", "Reports")',
        data: frmData,
        async:false,
        success: function (data) {
            console.info('succesfully called back');
            var win = window.open('', rptName, '_blank');
            console.info('opening window');
            win.document.write(data);

        },
        error: function (x, y, z) {
            console.info(x + ' ' + y + ' ' + z);
        }
    });
}

出现此错误是因为系统中禁用了弹出窗口。因此,通过系统设置选项启用弹出窗口。

I have the following function, which works fine on a few PCs that I've tested on. I've tested this on Chrome, IE & Firefox with no issues. However, there's 1 particular PC (running Chrome), that throws this error "Uncaught TypeError: Cannot read property 'document' of undefined" on the line:

                win.document.write(data);

Could it be because win is null?

If so, why is this the case on this particular PC?

Is there some Chrome settings that needs to be set?

Method:

    function viewReport() {
        console.info('generating event report');
        var frmData = $('#frmEventReport').serializeArray();
        var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
        console.info('generated random report name ' + rptName);
        $.ajax({
            //type: "GET",
            timeout: 120000,
            url: '@Url.Action("EventReport", "Reports")',
            data: frmData,
            success: function (data) {
                console.info('succesfully called back');
                var win = window.open('', rptName, '_blank');
                console.info('opening window');
                win.document.write(data);

            },
            error: function (x, y, z) {
                console.info(x + ' ' + y + ' ' + z);
            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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