niutech / showModalDialog

window.showModalDialog polyfill using a <dialog> element
https://niutech.github.io/showModalDialog/demo.html
Apache License 2.0
177 stars 88 forks source link

Problems IE8 and older #21

Closed erikjandelange closed 8 years ago

erikjandelange commented 8 years ago

Array.filter() isn't included in IE until version 9. So the script stops at filter and doesn't work in IE8 or older. When the script stops it doesn't recognizes the function dialog.close().

To solve this issue replaced the following lines.

//if using eval
var isNext = false;
var nextStmts = caller.split('\n').filter(function(stmt) {
if(isNext || stmt.indexOf('showModalDialog(') >= 0)
    return isNext = true;
    return false;
});

with

//if using eval
var isNext    = false;
var allStmts  = caller.split('\n');
var nextStmts = [];
for (i = 0; i < allStmts.length; i++) {
    var stmt = allStmts[i];
    if( isNext || stmt.indexOf('showModalDialog(') >= 0) {
        nextStmts.push(stmt);
        isNext = true;
    }
}

Also the addEventListener isn't supported in IE8 or older. To fix this issue you can use:

if( document.getElementById('dialog-close').attachEvent ) { // IE DOM
    document.getElementById('dialog-close').attachEvent('onclick', function() {
        e.preventDefault();
        dialog.close();
    });
} else {
    document.getElementById('dialog-close').addEventListener('click', function(e) {
        e.preventDefault();
        dialog.close();
    });
}
niutech commented 8 years ago

Thank you for the solution. However, IE natively supports showModalDialog, so there is no need for the polyfill. It is only for Google Chrome, which has removed showModalDialog.