The goal
Send true
or false
when window is closed.
The problem
When I click on a button, a window is opened with window.open();
syntax. What I need seems to be simple: when the window is closed, return to the window that opened the popup a response from the server, that can be true
or false
— like the Facebook's API does.
Someone knows how can I do this in a simple way?
Spotlight
I don't want to use jQuery because the page's CSS is overwriting the popup's CSS.
Current syntax
HTML:
[...]
<a href="#" class="share" data-networkName="<?php echo $network->name; ?>">
Share</a>
[...]
JS:
$(".share").on("click", function(event) {
event.preventDefault();
var networkName = $(this).data("networkName");
window.open("share.php?network=" + networkName");
});
The goal
Send true
or false
when window is closed.
The problem
When I click on a button, a window is opened with window.open();
syntax. What I need seems to be simple: when the window is closed, return to the window that opened the popup a response from the server, that can be true
or false
— like the Facebook's API does.
Someone knows how can I do this in a simple way?
Spotlight
I don't want to use jQuery because the page's CSS is overwriting the popup's CSS.
Current syntax
HTML:
[...]
<a href="#" class="share" data-networkName="<?php echo $network->name; ?>">
Share</a>
[...]
JS:
$(".share").on("click", function(event) {
event.preventDefault();
var networkName = $(this).data("networkName");
window.open("share.php?network=" + networkName");
});
Share
edited Oct 1, 2013 at 8:45
C3roe
96.8k15 gold badges98 silver badges170 bronze badges
asked Sep 30, 2013 at 23:35
Guilherme OderdengeGuilherme Oderdenge
5,0116 gold badges66 silver badges96 bronze badges
2 Answers
Reset to default 7This is what I came up with:
UPDATE
receive.html
<a href="#" onclick="openWindow()">Share</a>
<script>
var new_window = null;
function openWindow() {
new_window = window.open('return.html');
}
// Callback Function that we will call in child window
function sendMessage(message) {
alert(message);
new_window.close();
}
</script>
return.html
<a href="#" onclick="window.messageParent();">Mark As Shared</a>
<script>
function messageParent() {
// Calls sendMessage function on the parent window.
window.opener.sendMessage("Hello World!");
}
</script>
You could then handle the return value that you would like to in the sendMessage function in the parent window.
This is the simplest method I could e up with. Please let me know if this works.
Try this:
$(".share").on("click", function(event) {
event.preventDefault();
var networkName = $(this).data("networkName");
window.onbeforeunload = function() {
window.open("share.php?network=" + networkName");
}
});
UPDATE
main.php's script:
$(".share").on("click", function(event) {
event.preventDefault();
var networkName = $(this).data("networkName");
window.onbeforeunload = function() {
window.open("share.php?network=" + networkName");
}
function send(msg) {
//send msg to db or store as cookies
}
});
popup.html's script: [Let's say you have a share button called '#popup-btn']
$('#popup-btn').click(function() {
window.opener.send('MSG SENT FROM POPUP {THEY SHARED SOMETHING}');
});