How can I make window.showmodaldialog work in chrome 37?
Solution 1
I put the following javascript in the page header and it seems to work. It detects when the browser does not support showModalDialog and attaches a custom method that uses window.open, parses the dialog specs (height, width, scroll, etc.), centers on opener and sets focus back to the window (if focus is lost). Also, it uses the URL as the window name so that a new window is not opened each time. If you are passing window args to the modal you will need to write some additional code to fix that. The popup is not modal but at least you don't have to change a lot of code. Might need some work for your circumstances.
<script type="text/javascript">
// fix for deprecated method in Chrome 37
if (!window.showModalDialog) {
window.showModalDialog = function (arg1, arg2, arg3) {
var w;
var h;
var resizable = "no";
var scroll = "no";
var status = "no";
// get the modal specs
var mdattrs = arg3.split(";");
for (i = 0; i < mdattrs.length; i++) {
var mdattr = mdattrs[i].split(":");
var n = mdattr[0];
var v = mdattr[1];
if (n) { n = n.trim().toLowerCase(); }
if (v) { v = v.trim().toLowerCase(); }
if (n == "dialogheight") {
h = v.replace("px", "");
} else if (n == "dialogwidth") {
w = v.replace("px", "");
} else if (n == "resizable") {
resizable = v;
} else if (n == "scroll") {
scroll = v;
} else if (n == "status") {
status = v;
}
}
var left = window.screenX + (window.outerWidth / 2) - (w / 2);
var top = window.screenY + (window.outerHeight / 2) - (h / 2);
var targetWin = window.open(arg1, arg1, 'toolbar=no, location=no, directories=no, status=' + status + ', menubar=no, scrollbars=' + scroll + ', resizable=' + resizable + ', copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
targetWin.focus();
};
}
</script>
Solution 2
From http://codecorner.galanter.net/2014/09/02/reenable-showmodaldialog-in-chrome/
It's deprecated by design. You can re-enable showModalDialog support, but only temporarily – until May of 2015. Use this time to create alternative solutions.
Here’s how to do it in Chrome for Windows. Open Registry Editor (regedit) and create following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\EnableDeprecatedWebPlatformFeatures
Under the EnableDeprecatedWebPlatformFeatures
key create a string value with name 1
and value of ShowModalDialog_EffectiveUntil20150430
. To verify that the policy is enabled, visit chrome://policy URL.
UPDATE: If the above didn’t work for you here’s another method to try.
- Download Chrome ADM templates from http://www.chromium.org/administrators/policy-templates
- Extract and import policy relevant to your locale (e.g. windows\adm\en-US\chrome.adm. You can import either via
gpedit.msc
or using these utilities on Home editions of windows: http://blogs.technet.com/b/fdcc/archive/2008/05/07/lgpo-utilities.aspx) - Under “Adminstrative Templates” locate Google Chrome template and enable “Enable Deprecated Web Platform Feautes”.
- Open the feature and add “ShowModalDialog_EffectiveUntil20150430″ key.
Solution 3
A very good, and working, javascript solution is provided here : https://github.com/niutech/showModalDialog
I personnally used it, works like before for other browser and it creates a new dialog for chrome browser.
Here is an example on how to use it :
function handleReturnValue(returnValue) {
if (returnValue !== undefined) {
// do what you want
}
}
var myCallback = function (returnValue) { // callback for chrome usage
handleReturnValue(returnValue);
};
var returnValue = window.showModalDialog('someUrl', 'someDialogTitle', 'someDialogParams', myCallback);
handleReturnValue(returnValue); // for other browsers except Chrome
Solution 4
This article (Why is window.showModalDialog deprecated? What to use instead?) seems to suggest that showModalDialog has been deprecated.
Solution 5
The window.returnValue property does not work directly when you are opening a window using window.open() while it does work when you are using window.showModalDialog()
So in your case you have two options to achieve what you are trying to do.
Option 1 - Using window.showModalDialog()
In your parent page
var answer = window.showModalDialog(<your page and other arguments>)
if (answer == 1)
{ do some thing with answer }
and inside your child page you can make use of the window.returnValue directly like
window.returnValue = 'value that you want to return';
showModalDialog halts the executions of the JavaScript until the dialog box is closed and can get a return value from the opened dialog box when its closing.But the problem with showModalDialog is that it is not supported on many modern browsers. In contrast window.open just opens a window asynchronously (User can access both the parent window and the opened window). And the JavaScript execution will continue immediately. Which bring us to Option 2
Option 2 - Using window.open() In your parent page write a function that deals with opening of your dialog.
function openDialog(url, width, height, callback){
if(window.showModalDialog){
options = 'dialogHeight: '+ height + '; dialogWidth: '+ width + '; scroll=no'
var returnValue = window.showModalDialog(url,this,options);
callback(returnValue)
}
else {
options ='toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height;
var childWindow = window.open(url,"",options);
$(childWindow).on('unload',function(){
if (childWindow.isOpened == null) {
childWindow.isOpened = 1;
}
else {
if(callback){
callback(childWindow.returnValue);
}
}
});
}
}
And whenever you want to use open a dialog. Write a callback that deals with the return value and pass it as a parameter to openDialog function
function callback(returnValue){
if(returnValue){
do something nice with the returnValue
}}
And when calling the function
openDialog(<your page>, 'width px', 'height px', callbak);
Check out an article on how to replace window.showModalDialog with window.open
Anubrij Chandra
Updated on July 26, 2020Comments
-
Anubrij Chandra almost 4 years
We have a huge web application where we use
window.showmodaldialog
for alerts, confirmations and popups. Since Chrome version 37 this call has been disabled.Is there any quick workaround to make
window.showmodaldialog
work in the latest version of Chrome?I am adding here a workaround for window.showmodaldialog, although this is not a perfect workaround because this will not break the code execution as showmodaldialog was doing, instead this will open the popups.
window.showModalDialog = function (url, arg, feature) { var opFeature = feature.split(";"); var featuresArray = new Array() if (document.all) { for (var i = 0; i < opFeature.length - 1; i++) { var f = opFeature[i].split("="); featuresArray[f[0]] = f[1]; } } else { for (var i = 0; i < opFeature.length - 1; i++) { var f = opFeature[i].split(":"); featuresArray[f[0].toString().trim().toLowerCase()] = f[1].toString().trim(); } } var h = "200px", w = "400px", l = "100px", t = "100px", r = "yes", c = "yes", s = "no"; if (featuresArray["dialogheight"]) h = featuresArray["dialogheight"]; if (featuresArray["dialogwidth"]) w = featuresArray["dialogwidth"]; if (featuresArray["dialogleft"]) l = featuresArray["dialogleft"]; if (featuresArray["dialogtop"]) t = featuresArray["dialogtop"]; if (featuresArray["resizable"]) r = featuresArray["resizable"]; if (featuresArray["center"]) c = featuresArray["center"]; if (featuresArray["status"]) s = featuresArray["status"]; var modelFeature = "height = " + h + ",width = " + w + ",left=" + l + ",top=" + t + ",model=yes,alwaysRaised=yes" + ",resizable= " + r + ",celter=" + c + ",status=" + s; var model = window.open(url, "", modelFeature, null); model.dialogArguments = arg; }
Just put this code in the head section of page.
-
sanjeev40084 over 9 yearsdo you know how to do fix this in Mac?
-
Yuriy Galanter over 9 years@sanjeev40084 I haven't tried it myself, but according to chromium.org/administrators/… under Preference Name
EnableDeprecatedWebPlatformFeatures
you set the value<array><string>ShowModalDialog_EffectiveUntil20150430</string></array>
-
Alexander Prokofyev over 9 yearsI believe you should name string value ShowModalDialog_EffectiveUntil20150430 instead of ShowModalDialog_EffectiveUntil201504301.
-
JonBrave over 8 yearsI think you should make clear that the IE path (
window.showModalDialog()
) blocks until dialog closes, while the "Other browsers" path returns immediately. If you need blocking behaviour, this is not going to work. -
Yuriy Galanter over 7 yearsGotta love downvotes on 2-year old answer with no explanation. But if you are still trying to use
showModalDialog
and vent your frustration via downvotes - I feel sorry for you. -
ErTR over 7 yearsIt seems IE has same problems too. If we override the function by removing the first
if
, it works in all 3 major browsers. -
Kiquenet over 7 yearsAny sample in jsfiddle ? jsfiddle.net/grj4x5k9 sample using HTML5 dialog (with this problems)
-
Kiquenet about 7 yearsSuggestion: sample usage? Any sample in jsfiddle?
-
Nadeemmnn Mohd over 6 yearsThanks this worked for me... and i voted your answer
-
Bhargav Rao almost 5 yearsPlease don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question and flag for undeletion.