Source Code: (back to article)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Popup Example</title>
<script>
var myPopup = null; // Initialize the popup variable.

// Function to open a popup
function openPopup() {
// Check if the popup already exists and is not closed
if (myPopup === null || myPopup.closed) {
myPopup = window.open("", "PopupWindow", "width=400,height=400");
// Set the content of the popup
myPopup.document.write(`
<html>
<head><title>Popup Content</title></head>
<body>
<h1>Welcome!</h1>
<p>This is your popup window.</p>
<button onclick="window.close()">Close Window</button>
</body>
</html>
`);
// Ensure the popup gets focus
myPopup.focus();
} else {
// Bring the already opened popup to the front
myPopup.focus();
}
}

// Function to close the popup
function closePopup() {
if (myPopup && !myPopup.closed) {
myPopup.close();
myPopup = null; // Reset the popup variable after closing it.