如何使一个页面加载两次?

我想通过url传值来判断是否重载,但是页面还是会不停的刷新,没有起到作用,是不是代码顺序有问题。

//获取参数值
var seid_load = util.parseUrl(window.location).seid || "";

$(window).bind('load',function(){

    if(seid_load){
        $(window).unbind('load',function(){
            window.location.reload();
        });
        console.log('1');
    }

    window.location.reload();
    console.log('2');
});

我的思路有两种:
1.通过url传值使页面onload两次
2.通过cookie来判断使页面onload两次
请教具体做法,thx

阅读 6k
2 个回答

因为你即使window.location.reload()之后,你当前的url没有变化,js代码始终都会重新开始执行
不考虑兼容的话,可以试试用pushState方法

html<a href="./a.html?reload">跳转至a页面</a>
javascriptvar search = window.location.search;
if( search == '?reload' ){    //如果有reload参数,则自动刷新一次
    window.history.pushState({}, 0, './a.html');    //记得这里去掉参数,否则就会不停刷新
    window.location.reload();
}

真没有搞清楚你要干什么

但是有一点,如果这段js在你的代码中,那么

window.location.reload();

一定会在页面完全加载后执行的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题