<ul dir="auto">
<li>Electron version: 1.4.6</li>
<li>Operating system: Windows 10 (Version 1607)</li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function handleWindowBeforeunload(event) {
    const confirmed = (dialog.showMessageBox(remote.getCurrentWindow(), options) === 1);
    if (confirmed) {
        remote.getCurrentWindow().close();
    } else {
        event.returnValue = false;
    }
}

window.addEventListener(&quot;beforeunload&quot;, handleWindowBeforeunload);"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">handleWindowBeforeunload</span><span class="pl-kos">(</span><span class="pl-s1">event</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-s1">confirmed</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">dialog</span><span class="pl-kos">.</span><span class="pl-en">showMessageBox</span><span class="pl-kos">(</span><span class="pl-s1">remote</span><span class="pl-kos">.</span><span class="pl-en">getCurrentWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-s1">options</span><span class="pl-kos">)</span> <span class="pl-c1">===</span> <span class="pl-c1">1</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">confirmed</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-s1">remote</span><span class="pl-kos">.</span><span class="pl-en">getCurrentWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        <span class="pl-s1">event</span><span class="pl-kos">.</span><span class="pl-c1">returnValue</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-smi">window</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">"beforeunload"</span><span class="pl-kos">,</span> <span class="pl-s1">handleWindowBeforeunload</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<h3 dir="auto">Expected behavior</h3>
<p dir="auto">The window should stay open except when button 1 is pressed in the message box.</p>
<h3 dir="auto">Actual behavior</h3>
<p dir="auto">Sometimes (~1 out of 10 times) the window will close, regardless of the button pressed in the message box.<br>
It happens more often when the developer tools are closed, and never happens when using the debugger to step over the code.</p>
<h3 dir="auto">How to reproduce</h3>
<p dir="auto">Use the code snipped above with any message box with &gt;= 2 buttons, with closed developer tools.</p>
<p dir="auto">The code above is loosely modeled in the way atom does their beforeunload handling.<br>
(<a href="https://github.com/atom/atom/blob/master/src/window-event-handler.coffee#L150">https://github.com/atom/atom/blob/master/src/window-event-handler.coffee#L150</a>)</p>
<ul dir="auto">
<li>Electron version: 1.6.11</li>
<li>Operating system: MacOS Sierra 10.12.5</li>
</ul>
<h3 dir="auto">Expected behavior</h3>
<p dir="auto">To prevent page transition, I wrote the code like this:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="window.addEventListener('beforeunload', function(e) {
    if (!confirm('Page closing, OK?')) {
        e.returnValue = 'false';
        return false;
    }
});"><pre class="notranslate"><span class="pl-smi">window</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">'beforeunload'</span><span class="pl-kos">,</span> <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-en">confirm</span><span class="pl-kos">(</span><span class="pl-s">'Page closing, OK?'</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-s1">e</span><span class="pl-kos">.</span><span class="pl-c1">returnValue</span> <span class="pl-c1">=</span> <span class="pl-s">'false'</span><span class="pl-kos">;</span>
        <span class="pl-k">return</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">By this coding, I expect to prevent page transition if user choose "Cancel" ondialog.</p>
<h3 dir="auto">Actual behavior</h3>
<p dir="auto">This code works if I show DevTools window for debugging.<br>
But once I close DevTools window, this code not works and page transition occurs.</p>
<h3 dir="auto">How to reproduce</h3>
<ol dir="auto">
<li>Create project with 2 pages (page A, page B).</li>
<li>Add above code on page A.</li>
<li>Add the link to page B on page A.</li>
<li>Run electron</li>
<li>Show DevTools Window and click the link. Then dialog shows, press cancel. Page transition is canceled.</li>
<li>Close DevTools and click the link. Transition occurs even if cancel is selected.</li>
</ol>