javascript 是否可以使用自定义函数覆盖 window.location 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6478617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to overwrite window.location function with a custom function?
提问by anonymous-one
Is it possible to disable, or even better, replace with a custom function, window.location
?
是否可以禁用,甚至更好地替换为自定义函数,window.location
?
This question is related: Disable a built-in function in javascript (alert)
这个问题是相关的:Disable a built-in function in javascript (alert)
While it works quite well for window.alert
, this does not work for window.location
.
虽然它工作得很好window.alert
,这不适合工作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.
我们希望能够找到一种方法来替换或禁用(替换是理想的,所以我们可以 AJAX 日志)window.location
......肮脏的广告商有时会使用它来从我们的网络资产中窃取人们。
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.
即使只在少数特定浏览器上工作的东西也可以,因为一旦被捕获(通过 AJAX 日志记录),我们可以相当快地对此采取行动。
采纳答案by Jared Farrish
I don't believe you can reassign window.location
. From MDN:
我不相信你可以重新分配window.location
. 来自MDN:
Summary
Returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.
概括
返回一个 Location 对象,其中包含有关文档 URL 的信息并提供更改该 URL 的方法。您还可以分配给此属性以加载另一个 URL。
https://developer.mozilla.org/en/DOM/window.location
https://developer.mozilla.org/en/DOM/window.location
As it takes a value like a property, how would you "reassign" the object/function to another value? I don't think it's possible due to the property behavior.
由于它需要一个像属性这样的值,您将如何将对象/函数“重新分配”给另一个值?由于财产行为,我认为这是不可能的。
回答by adam ac
try this
试试这个
var _window = {
location: "myLocation"
};
(function (window) {
console.log(window.location);
}(_window));