Jump to content

How to disable parent window


php_developer

Recommended Posts

Hello, I am using html and javascript to open a child window and want to disable the parent window when the child window is active, below is the code i am using: <html> <head><script type="text/javascript"> var popupWindow=null; function child_open(){ popupWindow =window.open('new.html',"_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 onclick="parent_disable();" onfocus="parent_disable();"> <a href="javascript:child_open()">Click me</a></body> </html>

 

 

But this is not working.

Link to comment
Share on other sites

can't be done, as far as I know. they specifically make sure javascript can't control javascript in other windows. if they didn't it'd be a serious security hazard.

 

imagine some malicious website waiting for users to visit them and then creating a tiny window with 0 width/height that opened banking websites (or any site with important personal information) and then grabbed your cookies for those sites. it then has a lot of access to some information you as a user don't want them have access to. of course high security sites nowadays have better systems that can't be thwarted by simple stolen cookies, nonetheless javascript still shouldn't be able to access other windows.

 

if you want actual in-browser popups there are things like alert() and confirm(). however those are severely limited in functionality and is quite disruptive to the normal flow of a site. I believe you're interested in things called modals, which are in-window popups. modals are pretty much absolutely positioned div elements (the popup) with another div element blanketing everything behind the popup so a user can't access anything else on the site directly.

Link to comment
Share on other sites

I don't know why onunload seems to fire when the child opens. That causes me some trouble. Also I have a problem if the child window is not a local file.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>popup window</title><style>#mask1{position:fixed;top:5px;left:5px;right:5px;bottom:5px;background-color:gray;opacity:0.50;filter: alpha(opacity=50);display:none;border-radius:5px;}</style><script>var pop1 = null;window.onload = init;function init() {document.getElementById('out').innerHTML = '';document.getElementById('childlink').onclick = child_open;} function child_open() {var m = document.getElementById('mask1');m.style.display='block';m.onclick = refocus;//pop1 = window.open('http://www.w3schools.com',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280");pop1 = window.open('window1.html',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280");pop1.onunload = pop1.beforeonunload = recover1;// this is from dsonesuk}function recover1(){document.getElementById('out').innerHTML += '.';setTimeout(later1,500);}function later1(){if( pop1 && pop1.closed ){document.getElementById('mask1').style.display='none';}}function refocus(){pop1.focus();}</script></head><body><h3>This is the Main Window</h3><a href="#" id="childlink">Open Child</a><p>The main window content</p><div id="out"></div><div id="mask1"></div></body>    </html>
Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...