Electron: prevent / cancel page navigation


Is there anyway to prevent or cancel page navigation in electron?

win.webContents.on(‘did-start-loading’, function(event, url) {
if (event.sender.getURL().startsWith(‘http://xyz’)) {
The code above doesn’t work since the event handler gets executed while the page keeps on navigating away.

Similarly, I’d also like to do the same for event ‘did-get-redirect-request’, to prevent certain redirect from happening.

Many thanks


Doesn’t a simple return false; do it?

win.webContents.on('event', (e, url) => {
  if (condition) {
    return false;


I’ve answered these questions on StackOverflow: https://stackoverflow.com/questions/41068295/electron-prevent-cancel-page-navigation


It didn’t work unfortunately.