Javascript popup window in background after userinteract - javascript

I want to open a little popup after clicking on a button.This Popup should open in the background.
This is my Code
document.getElementById('btn').addEventListener('click', function() {
var newWindow = $this.open('/popup',"MyTitle", "width=600,height=400,scrollbars=yes,resizable=yes,location=no");
newWindow.blur();
this.window.focus();
}, false);
What is my mistake? Why does it not open in the background?

Have you tried window.focus(); instead of this.window.focus(); ? I'm unsure if this will work as most browsers it causes security issues. Popunders aren't popular!

Related

window.close() function in javascript

I have a problem with window.close() function. I have a simple HTML script:
<body>
gmail<br />
google<br />
<input type="submit" id="submit" name="submit" value="submit">
<script src="project.js"></script>
</body>
I want to open the above url's in different windows. Once the url's are loaded, the windows should close itself without any alerts. The below javascript code lets the url's open in different windows. But it closes only 'google.com' and the html webpage. The 'gmail' website remains open.
var submit = document.getElementById("submit");
submit.onclick = function() {
var getLinks = document.getElementsByClassName("one");
for(var i=0; i<=(getLinks.length-1); i++) {
window.open(getLinks[i]);
getLinks[i].onload = window.close();
}
}
What am I missing in this code. Any help would be appreciated.
Note: the links are only for test.
Check this out:
function openWin() {
myWindow = window.open("http://google.com", "myWindow", "width=200, height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
Easily found here:
http://www.w3schools.com/jsref/met_win_close.asp
Fiddle:
http://jsfiddle.net/zc2g7nkr/
It works fine in Chrome and i do not have to allow to block pop-ups.
It's not possible any longer to close a window you have not created.
Take a look at this window.close and self.close do not close the window in Chrome
The problem is that certain browsers do not allow you to close a window using javascript.
There are bugs that prevent it - specifically that the browser won't let code from one website close a window that is opened to a different website.
var win = window.open("http://www.google.com", '1366002941508','width=500,height=200,left=375,top=330');
setTimeout(function () { win.close();}, 3000);
Here is a DEMO
It works fine in Firefox when you allowing popup,
It opens a popup in chrome but it do not allow you to close a window using javascript.
A page to B page
window.open('B.html', '_blank');
window.open('','_self').close();
page close
window.open('','_self').close();

Javascript/JQuery: focus the popup window if already opened but not to reload it

Have the following code to open and focus the window popup, unless it hasn't opened yet. It works fine,
But the problem is after focus to the previous opened popup window, can't prevent reloading it and it makes loss of the data on it.
So how can prevent the existing window not to reload to retain the existing data when the link is clicked again?
window.open(url,"searchPatron","height=600,width=1000, status=yes,toolbar=no,menubar=no,location=no").focus();
Maybe it fits your needs:
DEMO
window['windows'] = {};
var url = "//testit.com";
$('button').click(function () {
var popup = window['windows'][url]? window['windows'][url]:
window['windows'][url] = window.open(url, "searchPatron", "height=600,width=1000, status=yes,toolbar=no,menubar=no,location=no");
popup.focus();
});

Opening a popup in background

I need to open an url in a pop up window, in the background. Currently in my code, the new windows pops up on top of the current window, without going in the background. I am running my code on firefox 23.0.1-fedora 18.
I have tried using .blur() and .focus() js methods, but they are not working.
Is there a clean and reliable solution for this, which will work for firefox and chrome?
http://jsfiddle.net/6La9W/1/
html
<button >click me</button>
js
$("button").click(function(){
var sOptions = 'target=_blank,toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1';
var popup = window.open('http://www.stackoverflow.com','',sOptions);
popup.blur();
});
Browsers don't allow this nowadays because peoples open illegal things in behind popup, and you shouldn't do this. but still you can do this and it is worked in IE only.
$("button").click(function(){
var sOptions = 'target=_blank,toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1';
var popup = window.open('http://www.stackoverflow.com','',sOptions);
window.focus();
});

pop up window opens fullscreen in firefox though sizes are given

I'm opening a Popup, it opens in internet explorer and mozilla firefox. But firefox ignores the given sizes, so a fullscreen popup is created.
Following code:
linkname
Following function:
<script type="text/javascript">
function popup (url) {
fenster = window.open(url, "Popup", "width=640,height=700,resizable=yes,scrollbars=yes");
fenster.focus();
return false;
}
</script>
I'm having a similar problem with a form submitted to a popup.
I have
<form name ="printView" method ="post" action="printOnly.php" target="popUp"
onsubmit="popup(this);" style="display:inline!important;">
(the style is to get the form on the same line as another form)
And the popup function is
function popup(form) {
window.open('','formpopup', 'view text',
'height=700,width=640,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes'
);
form.target = 'formpopup';
view.focus(this);
}
As you can see I have tried the top and left options, but my popup still opens at the same size as the main window, and the scrollbar, toolbar, menubar, directories and location settings are disregarded, I get only the location bar no matter what I set their values to.
PS. I checked Firefox popup preferences and they are all allowed.
This is what I have on a dev site which I know at works in Firefox (actually it's the only thing I have tested it on.
I don't know too much about js, so I'm sure I got this from the web somewhere- so hopefully it follows correct practices and what not.
// js:
<script type="text/javascript">
// Popup window code
function newPopup(url) {
popupWindow = window.open(
url,'popUpWindow','height=390,width=350,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
</script>
// link: edit: an onclick event should be used instead, but I'm leaving my original code.
Link text
You need to make the display property of your popup to be either display: block; or display: inline-block for your width and height dimensions to take effect.
linkname
will do what you want ;)

Refresh parent window when the pop-up window is closed

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();">

Categories