2

I have been using JavaScript popup windows, the windows were not saved as variables when I trigger them to open

Here is my code


function trig(){var pwin = window.open('https://stackoverflow.com/questions/68612238/popup-window-javascript','popUpWindow','height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes');document.querySelector('body').setAttribute('onclick', 'pwin.close()')};
<div onclick="trig()">Click here</div>



Here is an external link to try it
after clicking the Click Here button it should show a window, but I found out it doesn't save it as a variable. and then after the user presses the <body> element the window should close

Couldn't debug out any issues.

asked Aug 1, 2021 at 16:14
1
  • 1
    You need to declare the variable outside of your function. Also, if you're adding an onclick listener in your JS code, use the addEventListener method instead of setting an HTML attribute: document.body.addEventListener('click', () => pwin.close()); Commented Aug 1, 2021 at 16:21

2 Answers 2

1

pwin is not declared globally. You should instead be calling the close() method inside the function.

function trig() {
 var pwin = window.open('https://stackoverflow.com/questions/68612238/popup-window-javascript', 'popUpWindow', 'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes');
 document.querySelector('body').addEventListener('click', () => pwin.close());
};

It's closing immediately because when you click the 'Click here' button to open the window, the click event listener is added before the click event finishes firing, so it is immediately closed.

Instead, use setTimeout to add the event listener after, say, 50 milliseconds:

function trig() {
 var pwin = window.open('https://stackoverflow.com/questions/68612238/popup-window-javascript', 'popUpWindow', 'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes');
 setTimeout(()=>{document.querySelector('body').addEventListener('click', () => pwin.close());}, 50);
};
answered Aug 1, 2021 at 16:30
Sign up to request clarification or add additional context in comments.

3 Comments

Works but the window immediately closes, I need the addEventListener() to set AFTER user clicks and the window opens.
@Vivaan It does not close immediately for me. Can't reproduce what you're talking about.
Check the demo, It does that over there. dmgnqz.mimo.run/App.html
0

Looks like you need to declare a global variable, instead of a local variable

answered Aug 1, 2021 at 16:26

2 Comments

I didn't spot that!
When I used it, it didn't really use local variable. but in the post I didn't see

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.