JavaScript function works only in Internet Explorer (in ASP.NET WebForms)

Viewed 39

in aspx page (WebForms) I have a JavaScript function that works only in Internet Explorer and not in Edge/Chrome.

This one:

function ChoosePharmacy()
{ 
    var result = window.showModalDialog('search_pharmacy.aspx', window,"dialogWidth:800px;dialogHeight:600px;scroll: no;") 
    
    if (result != null)
            document.getElementById('hIdPharmacy').value = result;
}

This should open a pop-up window (with an aspx page inside), but because is an old legacy application, works only in Internet Explore, and now that users have move to Edge, has stop working.

Do someone has idea why?

Thanks a lot in advance.

Luis

1 Answers

From your description, I understand that you would like to know why your JS code is not working with modern browsers like Chrome or Edge.

If we refer to the document for Window.showModalDialog(), we could notice that it is deprecated and is not recommended to use on the sites.

enter image description here

As suggested by Elder, dialog is a replacement for window.showModalDialog().

If it is difficult to implement in your code then as an alternative, you could use the ShowModalDialog Polyfill.

You just need to add a reference to ShowModalDialog Polyfill in your code and your code will start working without any changes.

Example:

<!DOCTYPE html>
<html>
   <head>
      <title>Demo</title>
      <script src="https://unpkg.com/showmodaldialog"></script>
      <script>
         function ChoosePharmacy()
         { 
                    var result = window.showModalDialog('index.html', window,"dialogWidth:300px;dialogHeight:200px;scroll: no;") 
            
                    if (result != null)
                        document.getElementById('hIdPharmacy').value = result;
         }       
      </script>
   </head>
   <body>
      <h1>Example for ShowModalDialog Polyfill</h1>
      <br>
      <button onclick="ChoosePharmacy()">Call ChoosePharmacy Function</button>
   </body>
</html>

Output in the MS Edge browser:

enter image description here

Related