Can I cancel location.href in Javascript

It may sound silly, but I need this functionality.

Can I somehow cancel location.href = 'url' in Javascript after executing it?

For example, at the click of a button, I change the current page with some resource-intensive page, I want to give the user the opportunity to cancel it if loading the next page takes a lot of time.

+3
source share
2 answers

Yes you can. You can use the event to display a warning that the user can acknowledge or cancel. When a user cancels it, page navigation is canceled. beforeunload

:

window.addEventListener('beforeunload', (event) => {
  // Cancel the event as stated by the standard.
  event.preventDefault();
  // Chrome requires returnValue to be set.
  event.returnValue = '';
});

, , Chrome , returnValue , , HTML , event.preventDefault() .

+4

@Prutswonder - , . , . / .

, , /.

iFrames .

= > . ( onLoad) , iFrame .

, . iFrame .

+1

Source: https://habr.com/ru/post/1737651/


All Articles