Search code examples
javascriptasp.netpopup

Window.popup don't considers width and height


I have the following code to open a popup windows, problem is that it is opened as a new full-screen window and not popup and I don't understand why

dim url as string = "inserimento.aspx?inserimento=" & tipoInserimento

If Context.Request.Browser.IsMobileDevice Then
            target = "_self"
        Else
            target = "popup_window"
        End If

ClientScript.RegisterStartupScript(Me.GetType(), "popup", "popup('" + url + "','" & target & "','width=500,height=750,left=100,top=0,status=no, menubar=no, toolbar=no,resizable=no');", True)

The javascript function I use to get browser popup-block function:

 <script type="text/javascript">
        function popup(urlToOpen) {
            var popup_window = window.open(urlToOpen);
            try {
                popup_window.focus();
            }
            catch (e) {
                alert("Pop-up Blocker is enabled! Please add this site to your exception list.");
            }
        }
    </script>

Thank you for your help


Solution

  • You're calling popup() with all 3 parameters, but you've not included them when you actually call window.open(). Modify your script to include those parameters in the function signature and the call inside the function...

    function popup(urlToOpen, target, params) {
      var popup_window = window.open(urlToOpen, target, params);
      try {
        popup_window.focus();
      }
      catch (e) {
        alert("Pop-up Blocker is enabled! Please add this site to your exception list.");
      }
    }

    While this will resolve your immediate problem, I'd strongly recommend you look at some sort of modal dialog as popups are both frowned upon by most users, and blocked by default on most browsers.

    There are plenty of 3rd party examples around, and they're not difficult to create from scratch either. Have a look here for some examples using Bootstrap...

    https://getbootstrap.com/docs/4.0/components/modal/