Electron: prevent / cancel page navigation


#1

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’)) {
event.preventDefault();
}
})
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


#2

Doesn’t a simple return false; do it?

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

#3

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


#4

It didn’t work unfortunately.