Have a flash player that pops out into a separate popup browser window. And on the source page the flash player just displays a message that it is currently popped out.
Now if the user navigates away from the source page (to another page on the same domain) how do i get a reference to the popup or just detect if its open (using javascript on the new page)?
Figured this out. Had to have a javascript timer in the popped out window that trys to execute a function in the parent window that if exists will simply hide the player.
example
Popout window script
<script>
if(window.opener!=null)
setTimeout("popCheck();",2000);
function popCheck()
{
//Use try catch to prevent premission denied msgs in
//case parent window is on different domain
try
{
window.opener.setPoppedOut();
}
catch(e)
{
}
//Check Every 2 seconds
setTimeout("popCheck();",2000);
}
window.onbeforeunload = function()
{
//Used to facilitate popin on window close
if(window.opener!=null)
{
try
{
window.opener.setPoppedIn();
}
catch(e)
{
}
}
}
</script>
Parent window script
<script>
//Save the previous contents so that we can restore them later.
window.popoutContent = document.getElementById("popoutContent").innerHTML;
function setPoppedOut()
{
document.getElementById("popoutContent").innerHTML = "Player is Popped Out";
}
function setPoppedIn()
{
document.getElementById("popoutContent").innerHTML = window.popoutContent;
}
</script>
Related
I'm trying to find a way to detect if a Window has been closed, but the events :
onbeforeunload and onunload are only called if the page has been completely loaded.
Basically I'm trying to simulate the behavior of a modal popup window, that is, when the popup window is open, then the main page (the opener) is frozen by overlying a transparent div and when the popup is closed, in the onunload event, that div is hidden.
My problem is the if the content of the popup takes some time to load, and the user closes the window BEFORE it has finished loading then the onunload event is never fired and thus, the transparent div remains visible.
Do you know of any way I could detect if the page is being closed even if the it hasn't finished loading.
This is my code:
In the parent (opener) window
When the link is clicked:
popUpObj = window.open(url, 'ModalPopUp', opts);
popUpObj.focus();
LoadModalDiv();
Functions to show and hide the div
This functions are implemented in the parent window:
function LoadModalDiv() {
var bcgDiv = document.getElementById("divBackground");
bcgDiv.style.display = "block";
}
function HideModalDiv() {
var bcgDiv = document.getElementById("divBackground");
bcgDiv.style.display = "none";
}
In the child window (the popup)
<script type="text/javascript">
function OnClose() {
if (window.opener != null && !window.opener.closed) {
window.opener.HideModalDiv();
}
}
function OnLoad() {
if (window.opener != null && !window.opener.closed) {
window.opener.LoadModalDiv();
}
}
window.onload = OnLoad;
window.onunload = OnClose;
</script>
(1) i have a script that opens a new window (2)New window can change its location/url for some redirection (3) I want to execute window.postMessage to the final redirection page of the popup window which will the parent window will received
<script>
//open window
//pops will change its url for some redirection
var pops = window.open('http://est.com/test.php', 'Login with Test', "width=300px;heigth=500px;");
window.addEventListener("message",function(e) {
if (e.origin !== 'http://west.com') {
return;
}
alert(e.data);
},false);
</script>
<script>
//code below is from other origin 'http://est.com/finalredirect.php'
// 'http://est.com/finalredirect.php' is the final redirection page of the popup window
window.postMessage({'data': 'test'}, 'west.com');
</script>
Or is there another work around for this?
I have a button on a page1 which opens a new page2 in the existing window. When page2 is closed i want page1 to be refreshed.
It should be said that page1 is not the parent of page2 even though the button is triggered from page1.
You can open the 2nd window using javascript and set an interval to see if it is closed. When it is closed you can then refresh your page.
// Code goes here
var childWindow;
var timer;
function openWindow() {
childWindow = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
timer = setInterval(checkChild, 500);
}
function checkChild() {
if(childWindow && childWindow.closed) {
alert("Child window closed");
clearInterval(timer);
window.location.reload();
}
};
Try something like this, it should work
function buttonClick(url){
newWindow=window.open(url);
newWindow.onunload=function{ window.location.reload();};
}
Note: onunload event works only in IE and Firefox
Creating a small chat application where user can start chat by clicking on chat button next to user's profile.
Once anyone will click on chat button a popup window will appear. Now I want to detect dynamically if this popus is still open or closed.
If popup is opened= Display new message alert in notification.
if popup is closed = Do not alert new message in notification (As chat window is already opened)
This is what I tried:
Chat with user 2
Chat with user 1
// on document ready
setInterval(function(){
chkAlerts();
}, 10000);
var newwindow = null;
function popitup(url) {
if ((newwindow == null) || (newwindow.closed)) {
newwindow = window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
newwindow.focus();
}
else {
newwindow.location.href = url;
newwindow.focus();
}
}
function chkAlerts(){
if ((newwindow == null) || (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}
With this code I am always getting
alert('now window is closed show alerts');
chkAlerts will always show the alert as the initial value of newwindow is null.
Presuming you only want the alert after a chat window has been closed tweak the logic to ignore the initial null state;
function chkAlerts(){
if ((newwindow != null) && (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}
Your error is not in the javascript code, is in the anchor, you are closing the double quotes inside the call to the function, they must be like this:
Chat with user 2
Anyway, but also, as you can see, you are only storing the last opened window, if your interest is in know that both are opened you should create newwindow as an array.
Cheers,
Continued from comments:
Just so you know, when reloading or navigating away from the
window-opener page, the reference to newwindow will be lost.
Yes jan, this is the actual problem, do you think is there any
function in javascript to do this... or do i need play around with
session or something?
Not even possible with sessions or any other server sorcery.
Your option if you really need to keep track of the chat window is to include the chat on your "opener" page and make it an inline AJAX chat.
You could have an AJAX listener on all your pages and "open" that chat window on all open tabs/windows of your site, so that the user doesn't lose track of it.
The following code produces a javascript popup when a page is loaded and then closes the popup window when the parent window is closed.
<script language="JavaScript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
player.close();
}
window.onload = load;
window.onunload = unload;
</script>
This is working fine, however it closes the popup window when switching pages on the site. Is there any way this can be altered to keep the popup window until the actual URL/Site window is closed?
I think you have to check the status of the parent window from the child, instead of vice-versa.
This code has not been tested, but I'm taking a stab at the concept:
window.opener.onunload =
setTimeout(
function(){
if (!window.opener) self.close();
}, 1000);
Short answer is NO, the only way to hook into the event that closes the window is onunload, which fires when the document is unloaded (like when you navigate to another page).
Maybe you could check window.opener on the popup (with a timer, or onfocus), to check if the window that originated the popup still exists, then close the popup if it doesn't.
This may help you:
<script language="Javascript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
if (!(window.location.equals('http://yourSiteName.com'))) {
player.close();
}
}
window.onload = load();
window.onunload = unload();
</script>