是否可以替换" location.href"调用我自己的功能?

时间:2017-11-21 13:01:53

标签: javascript location-href

我将第三方游戏加载到我的包装系统,此游戏重定向到具有不同参数的不同位置。我需要做的是不允许它发生,而是在它试图捕获时,获取它设置的新字符串" location.href"相应地采取行动。在所有游戏代码执行完毕后,我可以在游戏的VM中运行我的JS。

有可能吗?当然,我搜索了互联网,但没有找到任何解决方案。


    /// here is the code I can not change, because it's a part of a 3rd party game I loaded:
    function goThere(){
       window.location.href = "google.com?some=thing";
    }

    /// here I can insert something, before the func above is invoked.
    var realLocation = window.location;
    /// I want the following function to be called every 
    ///time anyone attempts to change location.href:
    function onLocationChangeAttempt(newLocation){ 
       alert('Attempt to go to location '+newLocation+'! But we prevented it.'); 
    }
    window.location = { get:... set:...} // what goes here? It's the gist of my question.

    // and here it's invoked. 
    goThere();

1 个答案:

答案 0 :(得分:0)

您无法更改,但可以覆盖

goThere = function()
{
   //your logic
}

在浏览器会话中加载第三方库后,您需要添加此逻辑。

这样的东西
document.addEventListener( "DOMContentLoaded", function(){
    goThere = function()
    {
       //your logic
    }   
});