No. I’m afraid not. It would be a security risk if a browser allowed scripts to close arbitrary windows. Such a restriction as this exists to prevent malicious websites from disrupting user sessions or closing important tabs without user consent.
The closest you are going to get is to open the new window from within your existing window. For example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main Page</title>
</head>
<body>
<p>Click the button to open a new window</p>
<button id="openWindowButton">Open Window</button>
<script>
const openWindow = () => {
const newWindow = window.open("", "newWindow", "width=300,height=200");
newWindow.document.write(`
<html>
<head>
<title>New Window</title>
</head>
<body>
<p>Click the button to close this window</p>
<button id="closeWindowButton">Close Window</button>
</body>
</html>
`);
newWindow.document.close();
newWindow.addEventListener('DOMContentLoaded', () => {
const closeButton = newWindow.document.getElementById('closeButton');
newWindow.addEventListener('click', () => {
newWindow.close();
});
});
};
document.addEventListener('DOMContentLoaded', () => {
const openButton = document.getElementById('openWindowButton');
openButton.addEventListener('click', openWindow);
});
</script>
</body>
</html>
Bear in mind that depending on browser settings, this might open as a new tab, not a popup. It also relies on the user to instigate the action.
If this approach solves your problem, you could also consider moving the code for the new window into a file of its own.
HTML code is fired from a configuration in SAP application, which opens a new window, and what is required is that this new window has a close button to close the window that was opened, but this is what I see that due to restriction of JavaScript cannot be done
This now open a new windows, with the button “Close windows” . When i press this button, the window preview is closed but the window that have the button “Close windows” follow.