<ul dir="auto">
<li>Electron version: v1.4.2</li>
<li>Operating system: masOS 10.11.6 (El Capitan)</li>
</ul>
<p dir="auto">When an window that has <code class="notranslate">nodeIntegration: false</code> tries to open a Facebook login or share window, the window can not be closed. Neither with operating-system provided closing methods (close button, cmd+w) nor from the window itself (<code class="notranslate">window.close()</code>) is the window closeable. The Facebook window can also not be reloaded (cmd+r or <code class="notranslate">window.location.reload()</code> do nothing). There are no errors logged (neither windows nor to the main process).</p>
<p dir="auto">Steps to reproduce:</p>
<ul dir="auto">
<li>Given the example app below, start it with <code class="notranslate">electron .</code></li>
<li>Click on "Facebook"</li>
<li>The first time Facebook will prompt for login</li>
<li>Log in to Facebook</li>
<li>Try closing the Facebook popup window (might need to restart the application before this to see the bug happening)</li>
</ul>
<p dir="auto">Example app:<br>
index.html:</p>
<div class="highlight highlight-text-html-basic notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;a href=&quot;https://www.facebook.com/sharer/sharer.php?u=http://electron.atom.io/&quot; target=&quot;_blank&quot;&gt;Facebook&lt;/a&gt;
&lt;a href=&quot;about:blank&quot; target=&quot;_blank&quot;&gt;Blank&lt;/a&gt;"><pre class="notranslate"><span class="pl-kos">&lt;</span><span class="pl-ent">a</span> <span class="pl-c1">href</span>="<span class="pl-s">https://www.facebook.com/sharer/sharer.php?u=http://electron.atom.io/</span>" <span class="pl-c1">target</span>="<span class="pl-s">_blank</span>"<span class="pl-kos">&gt;</span>Facebook<span class="pl-kos">&lt;/</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span>
<span class="pl-kos">&lt;</span><span class="pl-ent">a</span> <span class="pl-c1">href</span>="<span class="pl-s">about:blank</span>" <span class="pl-c1">target</span>="<span class="pl-s">_blank</span>"<span class="pl-kos">&gt;</span>Blank<span class="pl-kos">&lt;/</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span></pre></div>
<p dir="auto">main.js:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="'use strict'

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

var mainWindow = null

app.on('ready', function() {
  mainWindow = new BrowserWindow({
    width: 1290,
    height: 800,
    webPreferences: {
      nodeIntegration: false
    }
  })

  mainWindow.loadURL(`file://${__dirname}/index.html`)
})"><pre class="notranslate"><span class="pl-s">'use strict'</span>

<span class="pl-k">const</span> <span class="pl-s1">electron</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'electron'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-s1">electron</span><span class="pl-kos">.</span><span class="pl-c1">app</span>
<span class="pl-k">const</span> <span class="pl-v">BrowserWindow</span> <span class="pl-c1">=</span> <span class="pl-s1">electron</span><span class="pl-kos">.</span><span class="pl-c1">BrowserWindow</span>

<span class="pl-k">var</span> <span class="pl-s1">mainWindow</span> <span class="pl-c1">=</span> <span class="pl-c1">null</span>

<span class="pl-s1">app</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'ready'</span><span class="pl-kos">,</span> <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-s1">mainWindow</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-v">BrowserWindow</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">width</span>: <span class="pl-c1">1290</span><span class="pl-kos">,</span>
    <span class="pl-c1">height</span>: <span class="pl-c1">800</span><span class="pl-kos">,</span>
    <span class="pl-c1">webPreferences</span>: <span class="pl-kos">{</span>
      <span class="pl-c1">nodeIntegration</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-s1">mainWindow</span><span class="pl-kos">.</span><span class="pl-en">loadURL</span><span class="pl-kos">(</span><span class="pl-s">`file://<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">__dirname</span><span class="pl-kos">}</span></span>/index.html`</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span></pre></div>
<p dir="auto">package.json</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  &quot;name&quot;: &quot;no-facebook&quot;,
  &quot;private&quot;: true,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;main&quot;: &quot;main.js&quot;
}"><pre class="notranslate">{
  <span class="pl-ent">"name"</span>: <span class="pl-s"><span class="pl-pds">"</span>no-facebook<span class="pl-pds">"</span></span>,
  <span class="pl-ent">"private"</span>: <span class="pl-c1">true</span>,
  <span class="pl-ent">"version"</span>: <span class="pl-s"><span class="pl-pds">"</span>1.0.0<span class="pl-pds">"</span></span>,
  <span class="pl-ent">"main"</span>: <span class="pl-s"><span class="pl-pds">"</span>main.js<span class="pl-pds">"</span></span>
}</pre></div>
<p dir="auto"><a href="https://github.com/etiktin/electron/blob/129d92b30acc2a778b65dc139f113cf10511c4f7/docs/api/browser-window.md">Current behavior</a> of beforeunload event in Electron may break existing web pages. However, changing the behavior according to the <a href="https://html.spec.whatwg.org/#prompt-to-unload-a-document" rel="nofollow">spec</a> may break existing Electron apps.</p>
<p dir="auto">This is a tradeoff proposed by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/deepak1556/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/deepak1556">@deepak1556</a> in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="100468939" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/2481" data-hovercard-type="issue" data-hovercard-url="/electron/electron/issues/2481/hovercard" href="https://github.com/electron/electron/issues/2481">#2481</a> to make both sides happy:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// do what Electron does currently: unload the page immediately.
window.addEventListener('beforeunload', function(ev) { ev.returnValue = 'true' });
// do what Electron does currently: prevent the unload.
window.addEventListener('beforeunload', function(ev) { ev.returnValue = 'false' });

 // do what the spec says: ask the user for confirmation if the string is not empty
window.addEventListener('beforeunload', function(ev) { ev.returnValue =
    a_string_other_than_true_false });"><pre class="notranslate"><span class="pl-c">// do what Electron does currently: unload the page immediately.</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-k">function</span><span class="pl-kos">(</span><span class="pl-s1">ev</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">ev</span><span class="pl-kos">.</span><span class="pl-c1">returnValue</span> <span class="pl-c1">=</span> <span class="pl-s">'true'</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">// do what Electron does currently: prevent the unload.</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-k">function</span><span class="pl-kos">(</span><span class="pl-s1">ev</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">ev</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-kos">)</span><span class="pl-kos">;</span>

 <span class="pl-c">// do what the spec says: ask the user for confirmation if the string is not empty</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-k">function</span><span class="pl-kos">(</span><span class="pl-s1">ev</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">ev</span><span class="pl-kos">.</span><span class="pl-c1">returnValue</span> <span class="pl-c1">=</span>
    <span class="pl-s1">a_string_other_than_true_false</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Non-string values, specifically <code class="notranslate">true/false</code>, are converted to their string forms.</p>
<p dir="auto">If <code class="notranslate">window.onbeforeunload</code> is used instead, the return value of the handler has same effect of <code class="notranslate">ev.returnValue</code>.</p>
<p dir="auto">A remaining question is what to do with <code class="notranslate">ev.preventDefault()</code>. The spec says the UA should ask the user for confirmation. However my test showed that Electron 0.30.4 on Windows allows reload even if <code class="notranslate">ev.preventDefault()</code> is called. So I think it is OK to implement <code class="notranslate">ev.preventDefault()</code> according to the spec?</p>