javascript - Window.location.href infinite loop - Stack Overflow

When using window.location.href I'm running into an infinite loop (even though it is placed inside

When using window.location.href I'm running into an infinite loop (even though it is placed inside a function that is only called once during startup).

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

window.location.hash works just fine (but I can't use that) ...

When using window.location.href I'm running into an infinite loop (even though it is placed inside a function that is only called once during startup).

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

window.location.hash works just fine (but I can't use that) ...

Share Improve this question edited Dec 8, 2014 at 20:25 BadFeelingAboutThis 14.4k2 gold badges34 silver badges40 bronze badges asked Dec 8, 2014 at 20:12 Ajax jQueryAjax jQuery 3452 gold badges5 silver badges19 bronze badges 2
  • 3 So every time the page loads, it redirects to itself... How could it possibly have a loop? – Travis J Commented Dec 8, 2014 at 20:15
  • 2 You need to add an if, to see if the window.location.href already includes the name= bit. If not included, run your window.location.href and have the page redirected. – quid Commented Dec 8, 2014 at 20:16
Add a ment  | 

2 Answers 2

Reset to default 5

You are creating your own loop.

On startup of the page you are calling:

window.location.href = ("?name=" + new Date().getTime());

Which causes the page to load itself with a new ?name=time appended on the end.

What you may want to do instead is change the hash part of the URL. Like so:

window.document.location.hash = new Date().getTime();

Otherwise you should conditionally call window.location.href so that it only executes at certain times, like so:

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 this function is run during startup and there is nothing conditional to stop it from being run, the window.location.href portion will execute each time, causing the looping you describe. You need to provide some condition that will stop the loop.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389324a4434698.html

相关推荐

  • javascript - Window.location.href infinite loop - Stack Overflow

    When using window.location.href I'm running into an infinite loop (even though it is placed inside

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信