I'm making an Electron app. This app has a way to open a new window (popup) to be dragged on a different monitor. This window has a way to open another one, and so on. I need the user to be able to close a window he does not want, but keep others open.
I am using target blank when creating the window, but the child window gets closed when the parent is closed.
Second, I tried binding to the new-window event in the main process. This is what I did:
let windows = [];
function createNewWindow(){
let win = new BrowserWindow(
{
width: 1600,
height: 900,
webPreferences: {
nodeIntegration: true
}
}
);
win.webContents.on('new-window', (event, url) => {
event.preventDefault()
let win = createNewWindow();
win.loadURL(url);
event.newGuest = win;
})
windows.push(win);
return win;
}
But the child gets closed when the parent is closed. I checked the windows var, but it is correctly retained in the main process.
How can I open a chain of windows without them being closed when the main parent is closed?
Related
I have a window example.com/welcome.php.
Using window.open(abcd.php,"mywindow","...") I open abcd.php in new window.
By using window.close() from abcd.php. I close abcd.php. How can I trigger some action in welcome.php when abcd.php closed,
You can use onunload event attached to the opened window;
var w = open('stackoverflow.com');
w.onunload = (ev) => {
console.log('unload', ev);
};
//w.close();
Update
Set unload even inside load event to prevent from unnecessary firing unload on window open, witch happen to me in chrome at least.
var w = open('https://stackoverflow.com/users/8424614/unnamedxaer');
w.onload = () => {
w.onunload = (ev) => {
console.log('unload', ev);
};
};
Update 2
The urls have to be in the same domain to work, otherwise you will get an error when trying to attach event to opened window (w) (set debugger in the line with onunload), they also must be served from same kind of server - not accessed like files when the url in browser looks like this c:/my-site/welcome.php.
Here is working example. Open CodeSandbox's console located at the bottom of the window below the browser section to see output.
I'm tying to have Multiple windows in my Electron app and each window should have its own applicationMenu. But when I open a new window, the functionalities in application menu such as reload and openDevTools only works for last window that just opened.
Why this is happening?
Code example:
app.on('ready',() => {
createWindow();
});
ipcMain.on('open-new-window',() => {
createWindow();
});
function createWindow() {
const window = new BrowserWindow({
width:900,
height:700,
webPreferences: {
nodeIntegration: true
}
});
window.loadUrl('index.html');
const menu = Menu.buildFromTemplate([{
label: "dev",
submenu: [{
label: "Refresh HTML",
click: () => {
window.reload();
}
}]
}]);
Menu.setApplicationMenu(menu);
}
When i open a new window from ipcMain, the reload() function only works in last opened window. Whether i click "Refresh HTML" in the first window or the second.
You have only one piece of application menu, which (from docs)
will be set as each window's top menu
So, when you overwrite it, this is the expected behavior. Let's say
You create window #1. window variable will refer to #1, thus your application menu will reload that.
You create window #2. window refer to #2 and you overwrite application menu to reload #2 upon click
Clicking on #1's menu will reload #2 anyway
To resolve this, you can reload the current window with BrowserWindow.getFocusedWindow(), this way your MenuItem's function won't depend on the reference of window.
Try
click: () => {
BrowserWindow.getFocusedWindow().reload();
}
I have a HTML page which I have opened in a new window. Now I want only that new window to be active until it is closed using close button (cursor should work on the new window only).
I have tried using window.focus() but I don't think it is the right way to do so.
var mywindow = window.open("editparameter.html", "Modify","height=600,width=800,left= 300,top = 100, location = no, modal=yes,alwaysRaised=yes");
mywindow.focus();
This is the code I have written in my current page. Here editparameter.html is the page that opens in the new window.
if you want to block your page or do something else while child tab is opened, you can open new tab and wait its close:
function open() {
// do something with current tab
const tab = window.open('https://stackoverflow.com/');
tab.onclose = onTabClosed;
}
function onTabClosed() {
// revert what has been done
}
I have creating a new browser window win by #new-window-idclick. And I have events system with that window. Closing win.closed for example. Everything is work until I go to links inside the created window.
$('#new-window-id').on('click', function(){
var finalUrl = $(this).attr('href');
var win = window.open(
finalUrl,
'fullWindowMode');
console.log('created '+ win.name);
var timer = setInterval(function() {
if(win.closed) {
console.log('closed '+ finalUrl);
clearInterval(timer);
document.getElementById('#iframeID').contentWindow.location.reload();
}
}, 1000);
});
So I have iframe on main page. I want to open it in a new window and edit a content there. Then after closing created window, my main page iframe should be updated.
Steps:
Click the link #new-window-id/ //window is opened.
Close window. win.closed is work!
When doesn't work
Steps:
Click the link #new-window-id/ //window is opened.
In the window edit blog post. (url was changed)
Close window. event win.closed doesn't work!
But it breaks if I surfing inside created window. Exists way to keep this connection?
I did the following:
1. Created a window let w = window.open('http://ya.ru')
2. Navigated in that window (within the same site)
3. Checked w.closed in the main window (false)
4. Then closed the popup and checked again (true).
Looks like w.closed works even after navigation.
But if you navigate to another origin in the new window, then location.reload() will become inaccessible from the main 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');