尽管返回false语句,window.location.href在无限循环中运行

时间:2016-05-30 14:00:00

标签: javascript jquery

当我在页面2.html上并点击.next按钮时,它会进入无限循环,而不是导航到页面3.html

/* Init current page */
var current_page = localStorage.getItem("current_page");

/* If current page is undefined set set it to first page */
if (current_page === null) {
    current_page = 1;
    localStorage.setItem("current_page", current_page); 
}

/* Load first page or where user had left off */
if (current_page != 1){
    window.location.href = current_page + ".html";
    return false
}


/* Navigate to next page */
$('button.next').on('click', function(e){
    e.preventDefault();
    current_page = parseInt(current_page)+1;
    localStorage.setItem("current_page", current_page);
    location.reload();
});

我在页面加载期间定义了window.location.href,因为如果用户退出并返回,则应该将用户带到最后访问过的页面。

1 个答案:

答案 0 :(得分:0)

在每个页面上设置一个与您的文件名编号匹配的变量activePage并与之进行比较,而不是在进行重定向之前始终与1进行比较

var activePage = 3;

if (current_page != activePage ){
    window.location.href = current_page + ".html";       
}

或者,您可以解析网址以从文件名

获取活动页码