【问题标题】:location.href require window.location.href on some pages [duplicate]location.href 在某些页面上需要 window.location.href [重复]
【发布时间】:2018-06-26 08:33:12
【问题描述】:

为什么我的 Web 项目中的某些页面只需在 onclick 函数中使用 location.href 即可工作,而有些页面需要我输入 window.location.href 才能进行重定向。 我已经阅读了与使用window 相关的scope 相关方面。 location.href 的前缀,但我仍然无法完全理解这一点。

任何有关这方面的信息都将是高度可观的。

干杯!

【问题讨论】:

标签: javascript


【解决方案1】:

window.location.href 起作用时,唯一会使location.href 不起作用的情况是,如果您隐藏 location 标识符,通过在比全局标识符更近的范围内声明一个标识符,像这样:

location.href = "..."; // would work here...
function foo() {
    var location = 42;
    location.href = "..."; // ...but not here

    function bar() {
        location.href = "..."; // ...nor here
    }

    bar();
}
foo();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多