[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5122
  • Last Modified:

Javascript modal dialog box - want it without cancel (X in top right corner) capability

I use a modal dialog in Javascript to create a "pop up dialog" that gets some data from the end user, using the following code:

function PopUp(url)
   if (window.showModalDialog)
      newwindow = window.showModalDialog(url,
          "PopUp",'dialogHeight:200px,dialogWidth:500px, status:no');
   } else {
      alert(' bad');
   return false;

the page loaded has two button images whos onclick method I use to return to the caller....all of this works fine except that I do NOT want user to be able to hit X on right top of window so as to cancel the window and leave my code in an unknown state. Anyone know how to prevent the showing of the "X" in top right corner, or how to disbale it?
2 Solutions
Not possible to disable the close button of the browser-popup-window. You can use the onunload function of the body to notify the open, that the windows was close without proper action like:

var action = false;
<body onunload="if(!action) window.opener.notify">

and in you onclicks for valid actions use:
onclick="action=true; ...rest of the onclick you use"
Let's look at how showModalDialog works first:

var returnValue = window.showModalDialog(url, arguments, features);

When the window closes, it returns a value.  If there is no return value set, it returns 'undefined'.

So, you can do this:
while(!showModalDialog(url, arguments, features))

Which will force the redisplay of the dialog box if it is closed using the close button.

However, this means inside your dialog box, you must make sure you are setting a return value.

<button onclick="window.returnValue='yes';window.close()">Yes</button>
<button onclick="window.returnValue='no';window.close()">No</button>


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now