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

Posted on 2007-07-31
Last Modified: 2008-04-14
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?
Question by:smmsmm57
    LVL 48

    Assisted Solution

    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; of the onclick you use"
    LVL 16

    Accepted Solution

    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>


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now