简体   繁体   中英

How do I prevent a script from changing the window.location or doing a reload on the current page?

I would like to prevent a third party script from changing the window.location and or reloading the current page with Javascript. Is there a way to monitor the location changes or window reload events and cancel them. I don't have control over the JavaScript library that does this and I need to temporary prevent page reloads during critical operations. I'm not trying to stop the user from reloading the page, just other scripts. jQuery solutions are welcome.

Thanks!

EDIT : the solution only needs to work in Firefox.

For preventing reload() you can do something like this:

var canReload = true;
var realReload = window.reload;
window.reload = function() {
  if (canReload) { 
    realReload();
  }
}

Basically override the reload function with your own. I'm not sure the same can be done with a property assignment (where window.location is set). You could look into custom getter/setter ( __defineSetter__ ) functions for Firefox, and create a setter for the location property of window .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM