De-activating windows using Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maria Bitsku

    De-activating windows using Javascript

    How do I deactivate a window using Javascript. For example if I have
    a window that opens up another window, how do I prevent the user from
    clicking (doing anything) in the original window until the new window
    has been closed. Any insight will help. Thank you very much in
    advanced.
  • Ivo

    #2
    Re: De-activating windows using Javascript

    "Maria Bitsku" <misscomplexity @hotmail.com> wrote in message
    news:90a9b4d1.0 309221040.2c9e6 f45@posting.goo gle.com...[color=blue]
    > How do I deactivate a window using Javascript. For example if I have
    > a window that opens up another window, how do I prevent the user from
    > clicking (doing anything) in the original window until the new window
    > has been closed. Any insight will help. Thank you very much in
    > advanced.[/color]

    onblur="self.fo cus()" in the popup will bring it to the front whenever
    another browser window is clicked. But user can still go to another program.
    Ivo


    Comment

    • Fotios

      #3
      Re: De-activating windows using Javascript


      "Maria Bitsku" <misscomplexity @hotmail.com> wrote in message
      news:90a9b4d1.0 309221040.2c9e6 f45@posting.goo gle.com...[color=blue]
      > How do I deactivate a window using Javascript. For example if I have
      > a window that opens up another window, how do I prevent the user from
      > clicking (doing anything) in the original window until the new window
      > has been closed. Any insight will help. Thank you very much in
      > advanced.[/color]

      Hi there,

      The kind of window you refer to is usually called a "modal window" or "modal
      dialog".

      Using self.focus() in the popup window (as suggested by Ivo) is a possible
      solution but not a very good one as:

      1. It will not work very well with some browsers.

      2. The popup window becomes obtrusive for other (non-browser or irrelevant
      browser) windows as well.

      3. You have to put this code in the popup window itself.

      Trying to assign a handler function to the onblur event of the newly open
      window (from the opener window) is still plagued with problems number 1 and
      2.


      Other solutions:
      ---------------

      * In IE there is showModalDialog () which creates a perfect modal dialog for
      you. There may be something similar in some other browsers but it is fair to
      say that this kind of solution would not cover a lot of browser real estate.

      * You can use something like the following which I just put together and
      have not tested with many browsers but please tell us of the results if you
      test it yourself. It should however cover a lot of browsers. Basically it
      tries to block all access to the opener window and puts focus on the popup
      instead:

      <script>

      //Should be at least mozilla and ie (maybe 4.0+) compatible.
      //You can try other event combos.
      //With Mozilla you can make the opener appear on top by clicking on its
      chrome;
      //this does not happen with IE 6.0 (and maybe older versions as well)

      w = window.open("po pup.htm", "", "height=200,wid th=200");


      window.onactiva te =
      window.onfocus =
      window.document .onfocus =
      window.document .onactive =
      window.document .onclick =
      window.document .onmousedown =
      window.document .body.onclick =
      window.document .body.onmousedo wn =
      window.document .body.onresizes tart =
      window.document .body.onbeforea ctivate =
      window.document .body.onfocusin =
      window.document .body.onactivat e =
      window.document .body.onbeforee ditfocus =
      window.document .body.ondrag =
      window.document .body.ondragsta rt =
      window.document .body.onscroll =
      window.document .body.onfocus =
      window.document .body.onclick =
      window.document .body.oncontext menu =
      window.document .body.ondblclic k =
      window.document .body.onselects tart =
      //add or remove events as needed

      w.onblur =

      function(e){

      if (!w.closed){

      //cross browser event killing
      if (e && e.cancelable){
      e.preventDefaul t();
      e.stopPropagati on();
      }
      else if (event){
      event.returnVal ue = false;
      event.cancelBub ble = true;
      }

      w.focus();
      }
      }


      </script>


      * You can use this other method that may however have adverse effects on
      popups that contain elements that receive focus. This method is also fairly
      CPU hungry. Additionally it will not prevent sneaky access to the opener
      window/document elements (although with some effort, sneaky access may be
      possible with the previous method as well). It may also have obtrusive
      effects to working with other windows. However, its big plus is that it will
      keep the popup on top no matter what (more appropriate if you are building
      an underground site with lotsa unsolicited popups).

      <script>

      w = open("popup.htm ", "", "height=200,wid th=200");

      setInterval("if (!w.closed) w.focus();", 100);

      </script>

      Overall I recommend solution number two. There are cases where such
      solutions will cause annoying side-effects but in my experience there are
      usually workarounds. Also note that the first script can probably be
      appropriately tweaked if you wish to use it with older browsers (like
      Netscape 4) as well.

      Best regards to the American military!

      Fotios


      Comment

      • Fotios

        #4
        Re: De-activating windows using Javascript

        [color=blue]
        >
        > w.onblur =
        >[/color]

        This line should have been omitted. Just comment it out.

        Cheers,
        Fotios


        Comment

        Working...