Pop Up Blocker in Chrome and IE - javascript

Below is the piece of code I am using to open a link in a new window say "abc".
If the user again clicks on the same link, it should close and reopen the link in the same window "abc".
window.openOrFocus = function(url, "abc") {
if (!window.popups) {
window.popups = {};}
if (window.popups["abc"]){
var v=window.open("", "abc");
v.close();}
window.popups["abc"] = window.open(url, "abc");
}
But Now, say I click on the link, it opens the URL in a new window named "abc".
Now I go and close the window "abc". and go back and again click on the link.
That time it shows up the pop up blocker.
I am confused as to why this pop up blocker is coming when the I go and manually close the window and try to reopen by clicking on the link.
Happens both in IE as well as Chrome

Probably because you're calling window.open with a blank URL or repeatedly in that case.
You don't need your window.open("", "abc") call; instead, just use the window reference you already have:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
};
I would also listen for the unload event so you can remove your reference:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
window.popups[windowName].onunload = function() {
delete window.popups[windowName];
};
};
Side note: This is a syntax error:
window.openOrFocus = function(url, "abc") {
// --------------------------------^
I've replaced it with windowName in the code above.

Related

How can i close tab or reopen popup tab in javascript?

I am trying to get the reference of the newly opened tab and use it in the same function to close the tab and reopen,
for more details:
I want to create a Java script button
on the first click open a new tab
on the second click of the same button close the tab and reopen the same tab
var Window;
// Function that open the new Window
function windowOpen() {
Window = window.open( "https://www.google.com/", "_blank", "width=400, height=450");
// if (window.opener) {
// window.opener.announceWindow( window );
// }
}
// function announceWindow( win ) {
// window.close();
// }
<button onclick="windowOpen()" target=_blank>
Open Window
</button>
You could use something like this:
let google;
let open = false;
function toggleWindow() {
open = !open;
if (open) {
google = window.open("https://www.google.com/", "_blank", "width=400, height=450");
} else {
google.close();
}
}
You can do like this:
var Window = window.open('https://www.google.com');
function windowOpen() {
if(Window !== null){
alert("Window alreay open");
Window.close();
Window = null;
}else {
Window = window.open('https://www.google.com');
}
}
<button onclick="windowOpen()">
Open Window
</button>
open() function opens a new tab. It's name property has _blank value by default. But if you close the tab manually, then you have to refresh the page to work JS properly otherwise you will get alert "Window already open" even after the closing of the tab.

Vue.js not detecting window close

I am trying to open a new window and then detect when that window has closed. But so far nothing I have tried is working. I have the following:
methods: {
submitForm: function(e) {
var newWindow = window.open('https://www.google.com', '_blank')
newWindow.onblur = this.windowClosing
}
windowClosing: function () {
console.log('tab closing')
}
}
The tab/window opens and what I would like to see is the "tab closing" in the parent window. I assume it might be writing it to the newWindow and then that is lost? I've also tried
window.addEventListener('xx')
Thank you

Check if browser opens window

I want to check if the browser opens the popup window. In case the browser denies opening the window, I want to take some other action.
This is my example code:
function open_window(){
window.open("url",_blank);
}
if(!open_window(){
//action
}
If the popup is blocked by browser, window.open will return null. So this function will return false.
function firepopup(url,width,height) {
n=window.open(url,'_blank','toolbar=0,location=0,directories=0,status=1,menubar=0,titlebar=0,scrollbars=1,resizable=1,width='+width+',height='+height);
return n==null;
}
You can try something like this:
var winRef;
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop')
//Some Another Action
}

Need to close all new window opened by my application

I have tried to close the new window by Window.close() this works for current tab close but I need to close all the new tabs opened by my application
var newwindow=null;
function NewWindow(mypage,myPagename)
{
params = 'width='+screen.width;
params += ', height='+screen.height;
params += ', top=0, left=0';
params +=', scrollbars=yes,menubar=yes,toolbar=yes';
newwindow=window.open(mypage,myPagename, params);
if (window.focus)
{
newwindow.focus()
}
return false;
}
Shall we close the new window by having their name , if yes means how ???
You can assign window.open() to a variable and then addEventListener to the window variable. Something like this:
var testWindow = window.open("enter/your/url");
testWindow.addEventListener('loadstart', yourListenerFunction);
Now when you want to close the window, you need to do this:
testWindow.removeEventListener('loadstart', yourListenerFunction);
testWindow.close();
removeEventListener() method removes the event listener previously registered with addEventListener().

Javascript detect popup window close

I have this code in my popup window (which is opened by parent window):
window.onbeforeunload = closeWindow;
function closeWindow(){
}
The problem is that this code fires when parent window is being refreshed. Is there a way for this code only to fire when popup window is actually being closed?
Hmmm. You could try something like this in the window that opens the popup.
var NewWin = window.open("NewWin", "example.htm", "width=100;height=300;");
// modify styling as necessary etc.
NewWin.onbeforeunload = function() {
window.setTimeout(function() {
if (!NewWin) {
// window has been closed
} else {
// false alarm, just a refresh
}
}, 1000);
}
EDIT: To prevent the window from reopening on parent page refresh, use a similar technique from within the popup
window.opener.onload = function() {
window.opener.NewWin = self;
}
Then change the first line of the code above to:
document.onload = function() {
if (!NewWin) { var NewWin = window.open("NewWin", "example.htm", "width=100;height=300;"); }
}

Categories

Resources