开发者

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

开发者 https://www.devze.com 2023-02-10 17:58 出处:网络
I would lik开发者_如何学JAVAe 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 win

I would lik开发者_如何学JAVAe 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消