Vue.js not detecting window close - javascript

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

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.

Only hide the window when closing it [Electron]

I try to hide my main window so that I hasn't to load again later.
I got the following code:
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// Emitted when the window is closed.
win.on('closed', (event) => {
//win = null
console.log(event);
event.preventDefault();
win.hide();
})
}
So that's not working for me, when I close the window I get this error message:
Can somebody help me? Line 37 is the line with win.hide()
Thank you!
Use the close event instead of the closed event.
When the closed event is fired the window is already closed.
When the close event is fired the window is still open and you can prevent it from closing by using event.preventDefault(); like this:
win.on('close', function (evt) {
evt.preventDefault();
});
However on MacOS that'll stop you from quitting your app. To allow quitting your app and preventing windows from closing use this code:
// Set a variable when the app is quitting.
var isAppQuitting = false;
app.on('before-quit', function (evt) {
isAppQuitting = true;
});
win.on('close', function (evt) {
if (!isAppQuitting) {
evt.preventDefault();
}
});
That'll only stop the window from closing if the app isn't quitting.

IE11 - Popup window launched from script will not close using JavaScript

I am relatively new to JS so apologies for any basic errors I have made here.
I am attempting to insert some JS on our site that will detect if the user has Flash enabled & the site is able to launch a popup, if these fail the user will be directed to a support page to resolve these.
The code works without issue on Chrome and Firefox, the issue I am having is on IE the popup which launches as a test is not being closed by the script.
Am I missing something glaringly obvious?
function loadpopunder(){
var popupBlockerChecker = {
check: function(popup_window){
var _scope = this;
if (popup_window) {
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function () {
_scope._is_popup_blocked(_scope, popup_window);
},250);
}else{
popup_window.onload = function () {
_scope._is_popup_blocked(_scope, popup_window);
};
}
}else{
_scope._displayError();
}
},
_is_popup_blocked: function(scope, popup_window){
if ((popup_window.outerHeight > 0)===true)
popup.close();
},
_displayError: function(){
popupFail=true;
}
};
var popup = window.open("http://www.google.com", '_blank', "width=10, height=10, left=1, top=1, scrollbars=no, resizable=no");
popupBlockerChecker.check(popup);
}
loadpopunder()

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

Categories

Resources