The perfect solution is to close the child window when the js parent window closes

  • 2020-03-30 02:43:53
  • OfStack

Recently, a permission management system was encountered. Because the permission management system and the original system style is not consistent, all new Windows open. The problem is that the permission management window does not close after admin logs out. After other ordinary users log in, they can still operate the permission management window.

Problem simplification: when admin logs out, or when main.html closes, all new Windows open are closed together. Problem solved

Just look at the code:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body onUnload="closeNews();"> 

<a href="#" onclick="openNew('http://www.baidu.com','nihao')"> Open a new window </a> 
<a href="#" onclick="logOut();"> Log out </a> 
</body> 
</html> 

<script> 
//The user records all open child Windows
var win_Array = new Array(); 
var win_num = -1; 

//Record every time a new window is opened
function openNew(uri,param){ 
win_num = win_num+1; 
win_Array[win_num] = window.open(uri); 
} 

//When the user logs out, the close method is executed. Body add onUnload = "closeNews();" When the main window closes, all child Windows close
function closeNews(){ 
//Turn off permission management
if(win_Array.length > 0){ 
for(var i = 0;i <= win_Array.length;i++){ 
var win_one = win_Array[i]; 
if(win_one != undefined){ 
win_one.close(); 
} 
} 
} 
} 

function logOut(){ 
//Close the child window
closeNews(); 

//Log out
} 

</script> 

For clarification:

< Body onUnload = "closeNews ();" > It is important to add an onUnload event on the body. That is, after the window closes, execute the method to close all child Windows.

Test: in the web project, ie8,ie10, firefox,chrome, and opera were all fine.

Ie has a problem with static main.html pages when you don't dump the wen container, because ie's window.open() actually opens a new page, not a new TAB. All window closing methods are no longer valid.

Time is limited, have not spent energy to investigate, also hope to know why the js technology god can guide. Thanks again.

Related articles: