How can i close tab or reopen popup tab in javascript? - 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.

Related

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

Is there a way to set focus to parent tab on opening a child tab?

The following code sets focus on the new tab, not to the opener.
function openChildtab() {
var win = window.open("https://www.amazon.co.uk/gp/product/B01J90O0N4/ref=sv_kinc_2", "_blank");
if (win) {
window.focus();
} else {
alert("Please allow popups for this website");
}
}
Open New tab

Pop Up Blocker in Chrome and IE

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.

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

javascript open parent window

The following is in the NewForm.aspx which gets executed from a parent page (surveys/lists/testsurvey/allitems.aspx). The javascript works and open google. However, it opens the google in the pop up window (newform.aspx) but i need to close the popup window and show google.com on the parent window (or whatever the parent window link is)
<script type="text/javascript">
function redirect()
{
var inputcCtrls = document.getElementsByTagName("input");
for(m=0; m<inputcCtrls.length; m++)
{
if(inputcCtrls[m].type == 'button' && inputcCtrls[m].value == 'Finish')
{
var funcOnClick = inputcCtrls[m].onclick;
inputcCtrls[m].onclick = function () { window.location = "http://www.google.com/" }; }
}
}
redirect();
</script>
Use the opener property to find the window that opened the current window:
window.opener.location = "http://www.google.com/";
window.close();

Categories

Resources