13

Is it possible to disable, or even better, replace with a custom function, window.location?

This question is related: Disable a built-in function in javascript (alert)

While it works quite well for window.alert, this does not work for window.location.

We would love to be able to find a way to replace or disable (replace would be ideal, so we can AJAX log) window.location... Dirty advertisers have at times before used this to steal people away from our web properties.

Any ideas?

Even something that only works on a few specific browsers would be fine as once caught (via AJAX logging) we can act on this fairly quickly.

4

2 回答 2

8

尝试这个

var _window = {
       location: "myLocation"
};

(function (window) {
   console.log(window.location);
}(_window));
于 2012-04-20T07:16:30.877 回答
6

我不相信你可以重新分配window.location。来自MDN

概括

返回一个 Location 对象,该对象包含有关文档 URL 的信息并提供更改该 URL 的方法。您还可以分配给此属性以加载另一个 URL

https://developer.mozilla.org/en/DOM/window.location

由于它需要像属性这样的值,您如何将对象/函数“重新分配”给另一个值?由于财产行为,我认为这是不可能的。

于 2011-06-25T15:33:37.727 回答