Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to open popup window and disable parent window

Tags:

I want to open a popup window and disable the parent window. Below is the code that I am using;

function popup() {      popupWindow = window.open('child_page.html','name','width=200,height=200');     popupWindow.focus(); } 

For some reason, the parent window does not get disabled. Do I need some additional code OR what is the case?

Again, I am looking for something similar to what we get when we use showModalDialog()..i.e. it does not allow to select parent window at all..Only thing is I want to get the same thing done using window.open

Also please suggest the code which will be cross-browser compatible..

like image 601
copenndthagen Avatar asked Apr 14 '11 08:04

copenndthagen


People also ask

How do I stop refreshing parent page when child opens window?

Set a flag once child window is open and unset it once its closed. In ShowPopup use event. preventdefault() if child window is open!

What is the JavaScript command for a popup window?

The syntax to open a popup is: window. open(url, name, params) : url.


1 Answers

var popupWindow=null;  function popup() {     popupWindow = window.open('child_page.html','name','width=200,height=200'); }  function parent_disable() { if(popupWindow && !popupWindow.closed) popupWindow.focus(); } 

and then declare these functions in the body tag of parent window

<body onFocus="parent_disable();" onclick="parent_disable();"> 

As you requested here is the complete html code of the parent window

<html> <head> <script type="text/javascript">  var popupWindow=null;  function child_open() {   popupWindow =window.open('new.jsp',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");  } function parent_disable() { if(popupWindow && !popupWindow.closed) popupWindow.focus(); } </script> </head> <body onFocus="parent_disable();" onclick="parent_disable();">     <a href="javascript:child_open()">Click me</a> </body>     </html> 

Content of new.jsp below

<html> <body> I am child </body> </html> 
like image 79
Anupam Avatar answered Sep 21 '22 02:09

Anupam