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..
Related
I have a JSP page A. And on page A, there are some links when click on them, page B or C will be popped up.
Now I have a javascript to run in page A. How can this javascript determine whether there are existing popup page B or C?
First of all you need to ensure that you have references to your opened windows available with you. It is currently not possible to get a list of already open window. If you are unsure you can refer to this answer that suggests a decent way of doing this.
Now, using the window object of the child window, you can check if it is null or not in order to know whether it is open or not:
var myWindow = window.open("https://www.google.com", "MsgWindow", "width=200,height=100");
function IsWindowClosed (win) {
return win.window == null;
}
console.log("Window is closed: ", IsWindowClosed(myWindow));
Here's a working fiddle: https://jsfiddle.net/6vhgpkmm/1/
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.
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.
Is it possible to check if same window is already opened?
For example i have opened a window via javascript.
Can i check if that's opened on another page via javascript?
Just want to focus on page if it's been opened already to avoid duplicate windows.
Thanks ;)
Look at window.open() method. You must specify the name of the window as the second parameter. If there already is a window with that name, then the new URL will be opened in the already existing window, see http://www.w3schools.com/jsref/met_win_open.asp
If you really want to check, if the window is opened by your own scripts, then you have to keep a reference to the opened window in a global variable or the likes and create it with
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
You can also encapsulate this behavior in a method:
var myOpenWindow = function(URL) {
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
myOpenedWindow.location.href= URL;
myOpenedWindow.focus();
}
And call that function with myOpenWindow('http://www.example.com/');
If you have parent--child window then here is a solution that will allow you to check to see if a child window is open from the parent that launched it. This will bring a
focus to the child window without reloading its data:
<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
popWin.focus();
} else {
popWin = window.open(url,'','width=800,height=600');
}
}
</script>
<a href="http://www.xzy.com"
onclick="popPage(this.href);return false;">link</a>
one more thing ::--- If the user refreshes the parent window, it may loses all its
references to any child windows it may have had open.
Hope this helps and let me know the output.
This will help if you want to open a url from a link
var Win=null;
function newTab(url){
//var Win; // this will hold our opened window
// first check to see if the window already exists
if (Win != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!Win.closed) {
Win.close();
// return winObj;
}
// otherwise fall through to the code below to re-open the window
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
Win = window.open(url);
return Win;
}
newTab('index.html');
Is there any way I can refresh the parent window when a popup window is closed without adding any javascript code to the popup window?
I have a page parent.php on which users can click "open popup" to open a popup window. This popup window shows some flash content and its not possible for me to add something like
window.onunload = function(){
window.opener.location.reload();
};
to the popup window page markup.
Is there any other method to achieve this?
Thanks
To make this work in all major browsers, you need to handle the unload event handler in the pop-up and do the reloading in the main window. In the main window, add
function popUpClosed() {
window.location.reload();
}
In the pop-up:
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.
I'm sure you can just add this to parent.php:
var myPop = "pop up window selector"
myPop.onunload = function(){
location.reload();
};
The problem with Tim Down's method is that it doesn't answer the original question. The requirement is that you cannot add any code to the pop-up window.
One solution that I've found, while not particularly elegant, is effective across all browsers I've tested on.
You will be simply polling the newly created window object continuously, checking if it's still open.
On parent window:
var register;
var poll;
function isOpen(){
if(register.closed){alert("Closed!"); clearInterval(poll);}
}
function create(){
register = window.open("http://www.google.com","register","width=425,height=550");
poll=setInterval("isOpen()",100); //Poll every 100 ms.
}
had similar problem to detect the closing popup in the parent window. I think the reason was
in not setting the document.domain property.
any way add to Tim Down answer the document.domain property for both window and popup like this:
<script type="text/javascript">
document.domain='<?=$_SERVER['SERVER_NAME']?>';
</script>
and instead of
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
I used in the popup :
<body onunload="window.opener.popUpClosed();">