I am calling the Javascript window.open()
function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks()
function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?
I am calling the Javascript window.open()
function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks()
function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('http://facebook./ekwebhost','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost. and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?
Share Improve this question edited Aug 13, 2013 at 16:28 RustyTheBoyRobot 5,9634 gold badges38 silver badges55 bronze badges asked Aug 13, 2013 at 15:42 MoboMobo 1181 gold badge1 silver badge7 bronze badges 5-
1
Look for
opener
. stackoverflow./questions/10591050/… – emerson.marini Commented Aug 13, 2013 at 15:43 - 5 Ugh. Pop-up windows, then pop-ups that appear when you close the pop-ups. Are you stuck in 1995? We did away with all this rubbish two decades ago. – Lightness Races in Orbit Commented Aug 13, 2013 at 15:47
- 2 Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to ekwebhost. and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs? – Mobo Commented Aug 13, 2013 at 17:27
- opening up another domain makes you lose the ability to know what is happening in the pop up. If you want someone to like something, you should probably use the facebook apis – epascarello Commented Jun 1, 2020 at 22:16
- Does this answer your question? How to call parent window function on close of child window in javascript? – zcoop98 Commented Oct 8, 2021 at 21:49
3 Answers
Reset to default 1function openWin(){
myWindow=window.open('http://facebook./ekwebhost','','width=600,height=400,left=200');
// Add this event listener; the function will be called when the window closes
myWindow.onbeforeunload = function(){ alert("Thanks");};
myWindow.focus();
}
Try this!
You can swap out the function(){ alert("Thanks");};
for MyThanks
to call your function.
The new window that you open will have an attribute called opener
that references the Window
object that created it. You can then call functions on the parent window by calling window.opener.MyThanks();
Popup:
<script type="text/javascript">
function closePopup() {
window.opener.MyThanks();
window.close();
}
</script>
<div>
<h1>My Popup</h1>
<!-- Form or info to display -->
<button onclick="closePopup();">Close</button>
</div>
While there is nothing inherently wrong with browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.
There are a bunch of ways to create dialogs within your webpage. jQuery UI is one library that I like.
You can try this:
<script>
function openWin() {
myWindow = window.open('http://facebook./ekwebhost', '', 'width=600,height=400,left=200');
myWindow.focus();
MyThanks();
}
function MyThanks() {
alert("Thanks");
}
</script>