Skip to content
Advertisement

Close Popup Before Opening Popup

I am attempting to improve an old calendar page I built using free “internet code” nearly 15 years ago. When the seven-column by six-row calendar displays, moving the mouse cursor over any one of the days displayed shows a “More Info” link, and clicking that link pops up a new window. The effective JS code:

<script type="text/javascript">
// Popup window code
function newPopup(url,striing) {
    popupWindow = window.open(url,striing)
}
</script>

url looks like ‘2022-11-27.html’ ‘striing’ is passed by the clicking of the link and looks like:

‘MyWindow’,’height=70,width=600, left=90,top=90,resizable=yes,menubar=no,scrollbars=yes, toolbar=no,menubar=no, location=no,directories=no,status=no’);

Each date link points to a separate HTML file that will appear in the popup window, and the “height” is individually set so that the popup window will contain the whole text of the html page without scrollbars or excess whitespace. What happens is that if “MyWindow” is not closed manually by the user with the mouse, the next call for date info will open in that same window. If the already open “MyWindow” was set for a short piece of text, the new html file will ignore a “height=400” parameter and load into the now too short window requiring scrollbars. If the already open “My Window” was set to be tall and the new html file is short it will leave the tall window with beaucoup whitespace at the bottom. I would very much like to do one of these things — shown in order of preference —

  1. code the window so that it will automatically fit the text in the html file without specifying height and if it is already open, adjust up or down to fit the new file.
  2. close the already open “MyWindow” with its incorrect dimensions before opening the newly called “MyWindow” with proper dimensions. Can either be done?

ardevain

I tried

<script type="text/javascript">
// Popup window code
function newPopup(url,striing) {
    popupWindow = window.close('MyWindow');
                      window.open(url,'MyWindow',striing)
}
</script>

but it didn’t even look like it had tried to work.

Advertisement

Answer

window.close does not accept a window name as the parameter.

Instead, store the newly opened window in a variable. Then, when creating a new window, close the window associated with that variable, then reassign it.

let prevPopup;
function newPopup(url, string) {
    prevPopup?.close();
    prevPopup = window.open(url,'MyWindow',string)
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement