admin管理员组文章数量:1323379
With the impending removal of the showModalDialog
API from various browsers, our pany like many others who provide large scale enterprise web applications are now faced with a significant dilemma.
Whilst we have centralised the calls to showModalDialog
down to 3 lines of code, we extensively rely on this code to provide feedback from modal user prompts (a quick search of the solution reveals around 2400 instances).
We could rip out showModalDialog
fairly easily and replace it with a Javascript/css based alternative, that's not a problem. The issue we face is that all of the calling code will no longer be blocking e.g.
if(doConfirm(...)) {
...
} else {
...
}
The above will simply fall through due to the introduction of a non-blocking alternative. We also cannot use the in-built blocking methods (alert, confirm) as the dialog buttons are customised in many cases and are also styled to fit in with our application.
Based on the above, are there any pragmatic workarounds/solutions that could be employed to avoid having to re-factor so much legacy previously blocking code?
With the impending removal of the showModalDialog
API from various browsers, our pany like many others who provide large scale enterprise web applications are now faced with a significant dilemma.
Whilst we have centralised the calls to showModalDialog
down to 3 lines of code, we extensively rely on this code to provide feedback from modal user prompts (a quick search of the solution reveals around 2400 instances).
We could rip out showModalDialog
fairly easily and replace it with a Javascript/css based alternative, that's not a problem. The issue we face is that all of the calling code will no longer be blocking e.g.
if(doConfirm(...)) {
...
} else {
...
}
The above will simply fall through due to the introduction of a non-blocking alternative. We also cannot use the in-built blocking methods (alert, confirm) as the dialog buttons are customised in many cases and are also styled to fit in with our application.
Based on the above, are there any pragmatic workarounds/solutions that could be employed to avoid having to re-factor so much legacy previously blocking code?
Share Improve this question edited Jul 11, 2014 at 9:01 Brett Postin asked Jul 11, 2014 at 8:54 Brett PostinBrett Postin 11.4k10 gold badges61 silver badges99 bronze badges 1- Possible duplicate of Why is window.showModalDialog deprecated? What to use instead? – Oriol Commented Aug 19, 2014 at 18:32
3 Answers
Reset to default 2You can avoid using callback functions by using my showModalDialog polyfill, which pauses execution of subsequent statements until the modal is closed. It does so by using generators, promises and the yield
keyword. It works in the newest Opera and Google Chrome.
You won't get around using asynchronous, event-based code.
pragmatic workarounds to avoid having to re-factor the code manually?
You can try a javascript-to-javascript piler that brings the await
keyword to js. It should automatically transpile your code to an asynchronous version.
Disclaimer: I haven't used any of these
In jQuery's 1.11.4 version, there is a built-in <dialog>
you can use, which also allows for capturing a callback parameter on close.
$("#dialog").dialog({
autoOpen: false,
width: 500,
height: 500,
modal: true
buttons: [
{
text: "Ok",
click: function () {
$(this).dialog("close");
}
},
{
text: "Cancel",
click: function () {
$(this).dialog("close");
}
}
]
});
Your value could be captured in the callback functions from the button click events.
You could even add HTML to append your own 'x' button to the existing "Close" button and hide the original, so you could do whatever you wanted:
$(document).ready(function () {
var closeHtml = '<a href="#" id="dialog-close" style="position: absolute; top: 0; right: 4px; font-size: 20px; color: #000; text-decoration: none; outline: none;">×</a>';
$("button[title='Close']").css('display', 'none').parent().append(closeHtml);
});
then attach a click
event to the dialog-close
ID from the 'x' button:
var url = 'https://www.cnn.';
// Link to open the dialog
$("#dialog-link").click(function (event) {
var dialog = $("#dialog");
dialog.dialog("open");
dialog.html('<iframe id="dialog-body" scrolling="yes" src="' + url + '" style="border: 0; width: 100%; height: 100%;"></iframe>');
$("#dialog-close").on('click', function (e) {
// ...do whatever you want here, then...
$("button[title='Close']").click();
//e.preventDefault();
//dialog.close();
});
event.preventDefault();
});
Only caveat, since this uses IFrame, it might not work if the security on the site prevents bringing in external sites to your own site, if you are using it in this way. Google, for example, prevents this use with their site.
This should be a cross-platform example - I've tested it in IE 11. "Polyfill", that I've seen others say is another way to do this, is NOT, and does NOT work in IE because it relies on Promise
, which is not supported in IE, as it shows at the bottom of this page: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
本文标签: javascriptRemoval of the showModalDialog APIStack Overflow
版权声明:本文标题:javascript - Removal of the showModalDialog API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121515a2421726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论