Core JavaScript

Open and Close a Window

This program demonstrates how to open and close a window in JavaScript.

WindowManagementAPI.html

<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net's Javascript</title>
		<style>
			button:disabled {
				background-color: #dddddd;
			}
		</style>
	</head>
	<body>
		<div style="width:300px;">
			<fieldset>
				<legend>Open and Close</legend>
				<button id="idOpen">Open a new window</button>
				<button id="idClose">Closed the window</button>
			</fieldset>
		</div>
	
		<script type="text/javascript">
			
			let gqWindow = null;
			
			const OpenWindow = () => {
				gqWindow = window.open('', 'Opened Window', 'width=400, height=400');
				
				if (gqWindow != null) {
					gqWindow.document.write('<h2>Created by the Window Management API</h2>');
					gqWindow.document.close();
					document.getElementById('idClose').disabled = false;
				} else {
					alert('New Window did not open!');
				}
			}
			
			const closeWindow = () => {
				if (gqWindow) {
					gqWindow.close()
					gqWindow = null;
					document.getElementById('idClose').disabled = true;
				}
			}
			// Set the button click handlers
			document.getElementById('idOpen').addEventListener('click', OpenWindow);
			document.getElementById('idClose').addEventListener('click', closeWindow);
		</script>
	</body>
</html>
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.