Programmatically disable window.location.reload?

Is there a way to override the default behavior for window.location.reload - make it no-op for debugging purposes?

+6
javascript
source share
2 answers

The problem is that for some reason location.reload effectively not a writable property in Firefox and Chrome. Here are some crazy ways I've come across to override it (and others) in these browsers. It uses the non-standard .__defineGetter__() method, in part to trick the magic of window.location = "/home.html" from interference.

 var _location = location; __defineGetter__('location', function() { var s = new String(_location); for(i in _location) (function(i) { s.__defineGetter__(i, function() { return typeof _location[i] == 'function' ? function(){} : _location[i]; }); s.__defineSetter__(i, function(){}); })(i); return s; }); __defineSetter__('location', function(){}); 

The resulting mock object should prevent any function call (including .reload ) or assignment (setting .href ) from actually taking effect. In addition, you can limit your testing to IE, Safari, and Opera, in which .reload is writable.

+7
source share

you need to call this code in the self-start function if it does not work.

 (function(location){ ... })(window.location); 
+3
source share

All Articles