I'm using Electron for a Desktop Application. I'd liked to open a different Window by a button on main window (index.html).
Therefore, I found BrowserWindow from the Electron API. But I'm totally new in Electron, and I try to get to know the whole Framework.
I have a "a"-link Tag on main window, which is my button. This should open a new defined Electron Window. Maybe by an onclick event?
If the new opened window is closed, on main window a defined Javascript Event like (reload();) should triggered.
So my question is. How is the best way to open a Window and how can i "log" in main window, when the new window is closed an fire my JS-Event.
Thanks
This might help you. It'll execute javascript on your Main Window when the New Window is closed.
newWindow.on('closed', function(e){
mainWindow.webContents.executeJavaScript(`alert("test");`);
})
Related
I'm web scraping in electron using puppeteer and whenever I encounter a download button with an onclick event triggering a 'downloadCourseDocs(....)' function, I emulate a click on it. This, in a normal browser opens a new tab for downloading that file.
In electron, I see that it opens a new blank, white window.
How do I prevent this/ hide the newly created window?
I tried playing around with the "browser-window-created" and "new-window-for-tab" events on the app and window instance but to no luck.
Thank you for your time.
In case someone is looking for an answer to this, here's how I fixed it:
window.webContents.on("did-create-window", (windowCreated) => windowCreated.hide());
You could probably also close the window.
This question already has answers here:
I need to open a new window in the background with JavaScript, and make sure the original is still focused
(6 answers)
Closed 9 years ago.
I have such situation. I try to open a window with window.open function new window was opened in a front of main window, how can i open it in background of main window, without focus on new. Is it possible to do such thing?
What you seek is called a "pop-under" window
Open a new window using let handle = window.open()
Lose focus of the new window by using handle.blur()
The return focus to your existing window using window.focus()
Example:
var handle = window.open('https://stackoverflow.com/');
handle.blur();
window.focus();
However, it's not a guarantee as user browser settings may override this behavior, especially pop-up blockers.
No you cant open window behind the main window. if you are using window.open() then it will be top of the main window. that's how the pop up windows works.
alternatively you can do this
Window.open();
yourMainWindow.focus();
Try using the Following
window.open("http://localhost/123", '_blank');
I am working on a web application where I am using JavaScript for the client side scripting. Now my requirement is to close all the opened window which were opened through window.showModalDialog().
For this, I read the history of the browser using window.history.length, but I do not know how to close each window. This works well for window.open(), but not for window.showModalDialog().
Could you please guide me to move forward?
You can close the opened window as follows:
To Open:
var window1=window.open("http://somedomain.com");
var window2=window.open("http://someotherdomain.com");
To Close
window1.close();
window2.close();
But be sure you call window1.close() and so on.. on the same script where you opened it.
You should not be using window.showModalDialog. Firefox has deprecated it, and Chrome has removed it. Also take a look at window.showModalDialog: What It is and Why You Should Never Use It.
The idea of showModalDialog is that all scripts are paused while the modal window is open. Thus, using window.close doesn't work, since as long as the window is open, no more scripts are being executed.
One possibility is to have JavaScript in the modal dialog so that is closes itself. You will not be able to close it from outside.
Is there anyone who can give me some thoughts on how to handle window to window communication using javascript givin that the two windows has no parent child relationship. Basically the other window is opened using window.open method. Any brilliant information is well appreciated.
assuming the following:
windowHandle=window.open('path/to/document');
you can interact between both windows.
You have a pointer to the window-object from the document where it was opened from using the variable-name:
//doSomething has to be known inside the new window
windowHandle.doSomething();
and from the document inside the new window to the window that opened the new window, using the opener-property:
//doSomething has to be known inside the window that opened the new window
opener.doSomething();
Anyone knows the difference?
The problem I met is that the page stops working correctly when opened by window.showModalDialog
window.showModalDialog vs window.open
Window.open will open up a new window through Javascript, with the URL and other features of the window that u pass as parameters. Here the parent window which opens the new window and the child window are independent windows.
Eg. Below
`window.open('winOpen.htm','name','height=255,width=250,toolbar=no,directories=no,status=no,
linemenubar=no,scrollbars=no,resizable=no');`
Window.showModalDialogue again works smilar to a window.open only diffrence being its a Modal window, It opens up as a new window but doesnt allow the user to access the parent window, unless you explicitly close it.
Here the child window is dependent on the parent window. If you close the parent window the child would also get closed.
window.showModalDialog("xpopupex.htm","name","dialogWidth:255px;dialogHeight:250px");
ShowModalDialogue windows can be used when u want the user to perform a particular action in the new window before he access the parent window again. like login before he can access the parent page..
tryed to make it as simple as possible...hope this help.. ;)