if window is popup - javascript

Is there a way to determine if the current window is a popup? This is what I have right now but for some reason it is not working. I am trying to make sure that certain pages are only shown in popup window.
if(!opener)
window.location = 'error.php';
The value of opener is [object DOMWindow] even though the window is not popup.

Assuming you're opening popup windows yourself- set a flag:
var myWindow = window.open(...);
myWindow.isPopup = true;
then, in your popup window, check for the flag:
if (!window.isPopup) {
window.location = 'error.php';
}

Related

Reference to parent window after allow popup window once

So I'm trying to test whenever popup windows are enabled for my site. My window should be blocked by browser's popup blocker, unless they are already allowed. If popup windows are allowed everything works fine, since I can test whenever the window was opened directly in my parent page.
Problem is that I still want to mark the test as sucessfull if the user allows to open popup window later by allowing the popup window to open. Only way for me to do that is by calling some function on my parent page, but window opened this way has null window.opener and I can't set the object on child window directly from my parent window since I don't know when will it open.
Is there any way to get reference to my parent window from child window when it's opened this way?
Or any other workaround that could call function on my parent window whenever the child window is sucessfully opened?
Maybe event event that I could hook up to whenever such window is opened, but I couldn't find any such event.
Sample code:
function () {
var myWin = window.open('popupWin.html', '_blank', 'resizable=no,scrollbars=no,height=1,width=1', true);
if (!myWin || myWin == null || typeof (myWin) == 'undefined')
SetPopupResult(false);
else {
SetPopupResult(true);
myWin.close();
}
}
popupWin.html is empty page with following script:
<script>
window.opener.SetPopupResult(true); //throws Cannot read property 'SetPopupResult' of null
setTimeout(function () {
window.close();
}, 1000);
</script>
Edit: Forgot to mention this is issue in Google-Chrome so far, firefox doesn't seem to have this problem and window.opener is correct, IE's behaivor is different as it refreshes the page and openes the popup window like they were enabled.

How to return focus on parent window from a pop window while loading first time

I am working on a website. Where while loading website first time opening index page with one pop window. First my problem is I want to minimize this pop window automatically while loading first time and focus return to website without any click in website.
Here is my code in index page I write this function:
myFunction();
function myFunction() {
//var myWindow = window.open("music.php", "", "width=320,height=60");
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
//myWindow.moveTo(0, 0);
}
And pop window having page extension music.php which should be minimize automatically and focus return to index page without any click on website.
I have already try with set the focus of a popup window to website everytime , but it was also not working.
Well, The window object has an opener property as well.
When we open the browser, that property is null, and when we open a popup , it points to the window the popup has been opened through.
I think something like this should suffice.
myFunction();
function myFunction() {
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
myWindow.opener.focus() // can also do window.focus()
}
The execution of JS should not stop, but if it does, you have to add the focus code to your child page/popup page.(Depending on varying browsers)
like
popupHTMLJS.js
focusParent();
function focusParent(){
parent = window.opener;
parent.focus()
}

closing the current popup and redirecting to the parent window

In JS I am using :
window.open('index.php?module=Contacts&action=UD_lookupemailtemplates&tempmode=userdefined&temp_url='+encodeURIComponent(temp_php_url),'emailtemplate','top=100,left=200,height=400,width=500,resizable=yes,scrollbars=yes,menubar=no,addressbar=no,status=yes')
To open a small window which has few links.
Now on the click on any one link an id will be attached to the a url and then I used this :
window.document.location.href = url;
This is also in JS. what this does is that it changes the contents of the small popup window but I need it to close the popup window and open this url in the main parent window from where the popup was created. Is that possible ?
Suppose
var popup_window = window.open(...);
For closing this use
popup_window.close();
and to redirect the parent windows
window.parent.location.href = url;
To close a popup that you opened, store the return value from window.open:
var popup = window.open(...);
Then, you can use:
popup.close();
I'm not clear based on your question where the URL variable is stored (in which window), but you can reference window variables in the popup via popup.variableName.
First you have to redirect parent window using
window.opener.location.href = "http://url.com";
and than close the current (popup) window
window.close();
Personaly I don't like pop up windows instead of that maybe yo can do the following:
You can open an overlay DIV that you can populate with data from "index.php?module=Contacts..." using AJAX and make links inside of that DIV that way so that by clicking them you close that overlay DIV and redirect page to what you want...

Detect browser window in javascript

I'm a newbie in javascript and I've to do the following operation: I have an asp .net application which load in a new window browser another content.
Through javascript, I need to refresh the popup which i opened before, but I don't know how I can find that new window.
I tried a simple window.location.reload(true); but this will cause a refresh only of the main browser window which launched the popup. Instead my intent is to refresh only the popup window.
When you open the window, give it a name (or store the return value of the window). Then, instead of window.location.reload, call name.location.reload where name is the name you gave the window.
If you open the window with window.open(), grab its return value. That's a reference to the window object of the popup. From there you can call location.reload(). For example, this will open a window and then reload it again:
var popup = window.open('/somepage');
popup.location.reload(true);
Store a reference to your popup window and then instead of calling the location.reload() function on your main window, call it on the popup window, like this:
var myNewWindow = window.open(...);
myNewWindow.location.reload(true);
Are you creating the new window using Javascript?
if you are using window.open(), that function returns a reference to the new window, So you can do something like this:
var newWin = window.open();
newWin.location.reload(true);

Javascript: Check for duplicate opened window

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');

Categories