De-activating windows using Javascript

M

Maria Bitsku

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.
 
I

Ivo

Maria Bitsku said:
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.

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

Fotios

Maria Bitsku said:
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.

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("popup.htm", "", "height=200,width=200");


window.onactivate =
window.onfocus =
window.document.onfocus =
window.document.onactive =
window.document.onclick =
window.document.onmousedown =
window.document.body.onclick =
window.document.body.onmousedown =
window.document.body.onresizestart =
window.document.body.onbeforeactivate =
window.document.body.onfocusin =
window.document.body.onactivate =
window.document.body.onbeforeeditfocus =
window.document.body.ondrag =
window.document.body.ondragstart =
window.document.body.onscroll =
window.document.body.onfocus =
window.document.body.onclick =
window.document.body.oncontextmenu =
window.document.body.ondblclick =
window.document.body.onselectstart =
//add or remove events as needed

w.onblur =

function(e){

if (!w.closed){

//cross browser event killing
if (e && e.cancelable){
e.preventDefault();
e.stopPropagation();
}
else if (event){
event.returnValue = false;
event.cancelBubble = 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,width=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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top