Detect if new window if opend using javascript - javascript

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.

Related

javascript open window url on exit

Hello I am trying to open a new URL on page exit in a browser window using javacript. My goal is when user closes the window to see the javascript alert box and when he press "Leave this Page" Instead ot the browser window to be closed to be redirected to google.com.
my code so far is the following:
<script type="text/javascript">
$(document).ready(function(){
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if (!areYouReallySure && !internalLink) {
areYouReallySure = true;
location.href="http://www.google.com"
return "Do you wish to go to google?";
}
}
window.onbeforeunload = areYouSure;
$('a').click(function(){
internalLink = true;
});
});
</script>
Rigth now when I chose leave this page it only closes the window.
This is impossible, and with good reason. Imagine if spam sites controlled what happens when you close their page. As a security measure, the only thing you can do when a user tries to navigate away from your page is display a message. This was implemented mostly so users can be warned that they haven't saved their data.

Prevent accessing parent window from child window using javascript [duplicate]

This question already has answers here:
How to disconnect JavaScript popup from opener
(2 answers)
Closed 6 years ago.
I show the links of different websites in my Page which when clicked, pops a new window.
All are working fine except one which creates trouble for me. say,when I click the link( eg, hyperlink for www.example.com), a new window pops out and when I press a HTML link from that page, the pop-up closes and the Parent window is loaded with that website( www.example.com ).
They have something in their code like
window.opener.location.href = "www.example.com";
this.window.close();
Is there any way by which we can prevent the child window from overriding the parent window?I don't want to use "onbeforeunload" because I don't want to pop an alert in the page.
Adding the Code snippet
<script type="text/javascript">
window.name = "parentWin";
var manipulate = document.referrer != null && (document.referrer.indexOf('upd.caqh.org') > -1);
if (window.opener != null && manipulate) {
if (window.opener.opener != null) {
window.opener.opener = null;
}
window.opener.location.href = "/OAS/Default.aspx?DCS_Relogon=1";
window.opener = null;
this.window.close();
}
</script>
Will you please provide a brief code?
As i know window.open()can use from the destination window to return details of the source (parent) window.
this.window.close();
Here, you might be referring to the class or id of page, which might be resulting in closing that page..

Window.open only if the window is not open

I have a link on my site that opens a new window to a page that plays a very long audio file. My current script works fine to open the page and not refresh if the link is clicked multiple times. However, when I have moved to a seperate page on my site and click this link again, it reloads. I am aware that when the parent element changes, I will lose my variable and thus I will need to open the window, overiding the existing content. I am trying to find a solution around that. I would prefer not to use a cookie to achieve this, but I will if required.
My script is as follows:
function OpenWindow(){
if(typeof(winRef) == 'undefined' || winRef.closed){
//create new
winRef = window.open('http://samplesite/page','winPop','sampleListOfOptions');
} else {
//give it focus (in case it got burried)
winRef.focus();
}
}
You should first to call winRef = window.open("", "winPopup") without URL - this will return a window, if it exists, without reloading. And only if winRef is null or empty window, then create new window.
Here is my test code:
var winRef;
function OpenWindow()
{
if(typeof(winRef) == 'undefined' || winRef.closed)
{
//create new
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop');
}
}
else
{
//give it focus (in case it got burried)
winRef.focus();
}
}
It works.
Thanks to Stan and http://ektaraval.blogspot.ca/2011/05/how-to-set-focus-to-child-window.html
My solution creates a breakout pop-up mp3 player that remains active site wide and only refreshes if the window is not open prior to clicking the link button
function OpenWindow(){
var targetWin = window.open('','winPop', 'sample-options');
if(targetWin.location == 'about:blank'){
//create new
targetWin.location.href = 'http://site/megaplayer';
targetWin.focus();
} else {
//give it focus (in case it got burried)
targetWin.focus();
}
}
Like you said, after navigating away from original page you're losing track of what windows you may have opened.
As far as I can tell, there's no way to "regain" reference to that particular window. You may (using cookies, server side session or whatever) know that window was opened already, but you won't ever have a direct access to it from different page (even on the same domain). This kind of communication between already opened windows may be simulated with help of ajax and server side code, that would serve as agent when sharing some information between two windows. It's not an easy nor clean solution however.

Executing function when pop-up window with redirects inside is closed

I'm showing the pop-up window from the page. The popup has one page, which then redirects to another (LinkedIn authorization one, to be clear), and then, after the successful login, the initial authorization page is opened again.
I want to reload the parent page when the popup is closed, but cannot do this.
The code is the following:
function OpenAuthorizePopUp() {
var w = window.open("AuthorizePage.aspx", "PopUp", "width=450,height=540");
w.onunload = function () {
SubmitPage();
};
return false;
}
function SubmitPage() {
alert("SUBMIT!");
}
The issue here is that SubmitPage() function is called not when window is closed, but just after the popup is shown. I guess it's because of redirect inside the popup, and unload is raised when we move from the first page.
Is there a way to catch the actual moment when the window is closed in this case?
So the flow of the popup goes like this:
1. Your page -> 2. LinkedIn Page -> 3. Your Page
Can you change the URL that LinkedIn sends you back to (3.) ? If so, have LinkedIn send you back to a page that has window.onunload = window.parent.SubmitPage; on it.
If you can't- ie, LinkedIn always sends you back to the first page- make the third page test for a successful connection to LinkedIn and if it's present, execute window.onunload = window.parent.SubmitPage;.
A possible improvement to your idea would be to just have the "success" page call window.parent.SubmitPage(); and then window.close(); since you won't need the popup anymore.
Found the solution. Instead of accessing parent window with window.parent, wrote
opener.location.reload(true);
And it works.

Force Close Webpage

I have a page (let's say page1) that open the page2 using:
showModalDialog(page2, '', 'dialogWidth:55em; dialogHeight:50em; status:0');
I am facing 2 problems (in this post i am going to write the first one):
1) when i want to close the second page using javascript code it always asks me that the page is trying to close itself do i want to allow that or no. I want to force close it without asking me anything. I have tried the following:
function CloseSelection(id) {
window.opener = "page1"
window.returnValue = id;
window.close();
}
And in the Form tag target="_self". But nothing happened, the popup keeps on showing.
can anyone help me please?
You dont need to set the window.opener as it is set by default to the object of opener page.
On Page2 the code will be:
function CloseSelection(id) {
window.returnValue = id;
window.close();
}
On Page1
var returnVal = showModalDialog("page2.html", '', 'dialogWidth:55em; dialogHeight:50em; status:0');
// here returnVal will contain the data which is return by page2
Hope it helps.
You can not force the window to open in a new window. It might work in some browsers but not all.
In order to close a window you can do this:
// open
var win = window.open("someurlgoeshere");
// close
win.close();
I believe you only get the "are u sure you wish the leave this page" message if the window itself tries to close itself (window.close())

Categories