Skip to content

e.preventDefault don't work on 'new-window' event? #2770

Closed

Description

@metainfa

I tested under Ubuntu 14.04 and Electron 0.31.2.

  const win = new BrowserWindow({width: 960, height: 540});
  win.loadUrl(url);

  win.webContents.on('new-window', (e, url, frame, tab) => {
    e.preventDefault();

    console.log('URL =', url);
}

When i click middle mouse button, i get: new Window (e.preventDefault don't working) and url into console.

Activity

  1. zcbenz commented on Sep 14, 2015

    @zcbenz
    Contributor

    Does it work as expected if you click with left mouse button?

  2. metainfa commented on Sep 14, 2015

    @metainfa
    Author

    Also don't work.

    win.webContents.on('will-navigate', (e, url) => {
        e.preventDefault();
    
        console.log('URL =', url);
    });

    I click with left mouse button by link and go to url (e.preventDefault don't work).

  3. zcbenz commented on Sep 14, 2015

    @zcbenz
    Contributor

    Can you share a small demo app? I'm not able to reproduce with your code snippet, so I'm wondering if it is caused by other parts of your code.

  4. metainfa commented on Sep 14, 2015

    @metainfa
    Author

    Ok.

  5. metainfa commented on Sep 14, 2015

    @metainfa
    Author

    I make demo app this link.

  6. zcbenz commented on Sep 14, 2015

    @zcbenz
    Contributor

    Calling event.preventDefault doesn't work in renderer process, because the callback is called asynchronously in renderer process and event.preventDefault has to be called synchronously. You can find detailed descriptions at http://electron.atom.io/docs/v0.32.0/api/remote/#passing-callbacks-to-the-main-process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions