小编sun*_*sen的帖子

如何在JavaScript中覆盖location.assign或location.replace函数?

我试图覆盖该location.assign函数,以便我可以确保它设置的URL始终是绝对的.但我似乎无法让它工作:当我使用Xmlhttprequest.open如下,它工作正常:

var oldOpen;
oldOpen = XMLHttpRequest.prototype.open;

// override the native open()
XMLHttpRequest.prototype.open = function(){

    //Prepend our proxyURL
    arguments[1] = prependURL+arguments[1];

    // call the native open()
    oldOpen.apply(this, arguments);
}
Run Code Online (Sandbox Code Playgroud)

但是location.assign这种技术不起作用.这就是我正在尝试的:

var old;
old = window.location.assign.prototype.constructor;

window.location.assign.prototype.constructor = function(){
    console.log('dd');
    console.log(arguments);
            alert('ff');
}

old.apply(this,arguments);
Run Code Online (Sandbox Code Playgroud)

当我运行这个(我正在Chrome中进行测试)时,结果是Uncaught TypeError: Illegal invocation.如何覆盖location.assign以获得我想要的行为?

javascript

4
推荐指数
2
解决办法
2128
查看次数

标签 统计

javascript ×1