Javascript Window.location.href无限循环

Javascript Window.location.href无限循环,javascript,Javascript,当使用window.location.href时,我正在运行一个无限循环(即使它被放置在一个函数中,在启动时只调用一次) window.location.hash工作正常(但我不能使用它).如果此函数在启动期间运行,并且没有任何条件阻止它运行,则每次都会执行window.location.href部分,导致您描述的循环。您需要提供一些停止循环的条件。如果此函数在启动期间运行,并且没有任何条件阻止它运行,则每次都会执行窗口.location.href部分,导致您描述的循环。您需要提供一些条件来停止

当使用
window.location.href
时,我正在运行一个无限循环(即使它被放置在一个函数中,在启动时只调用一次)


window.location.hash
工作正常(但我不能使用它).

如果此函数在启动期间运行,并且没有任何条件阻止它运行,则每次都会执行
window.location.href
部分,导致您描述的循环。您需要提供一些停止循环的条件。

如果此函数在启动期间运行,并且没有任何条件阻止它运行,则每次都会执行
窗口.location.href
部分,导致您描述的循环。您需要提供一些条件来停止循环。

您正在创建自己的循环

启动您正在调用的页面时:

window.location.href = ("?name=" + new Date().getTime());
这会导致页面在末尾附加一个新的
?name=time

您可能想做的是更改URL的哈希部分。像这样:

window.document.location.hash = new Date().getTime();
否则,您应该有条件地调用
window.location.href
,使其仅在特定时间执行,如下所示:

function onYouTubeIframeAPIReady() { // only called one time once API's are ready
    if (someVariable == "refreshNow") {
          window.location.href = ("?name=" + new Date().getTime()); //is EPOCH time
    }
}

您正在创建自己的循环

启动您正在调用的页面时:

window.location.href = ("?name=" + new Date().getTime());
这会导致页面在末尾附加一个新的
?name=time

您可能想做的是更改URL的哈希部分。像这样:

window.document.location.hash = new Date().getTime();
否则,您应该有条件地调用
window.location.href
,使其仅在特定时间执行,如下所示:

function onYouTubeIframeAPIReady() { // only called one time once API's are ready
    if (someVariable == "refreshNow") {
          window.location.href = ("?name=" + new Date().getTime()); //is EPOCH time
    }
}

所以每次页面加载时,它都会重定向到自身。。。它怎么可能有一个循环?您需要添加一个if,以查看window.location.href是否已经包含name=bit。如果未包含,请运行window.location.href并重定向页面。因此,每次加载页面时,它都会重定向到自身。。。它怎么可能有一个循环?您需要添加一个if,以查看window.location.href是否已经包含name=bit。如果没有,请运行window.location.href并重定向页面。我也会使用URL的哈希部分,因为它适合于时间戳。对于不进入哈希部分的内容,我会考虑HTML5“历史。推”特性。我也会使用URL的哈希部分,因为它适合于时间戳。对于没有进入哈希部分的东西,我会考虑HTML5“历史推”功能。