如何在javascript中显示窗口位置,当localStorage但不重新加载页面时

how window location in javascript when localStorage but not reload page?

本文关键字:新加载 加载 localStorage javascript 显示 窗口 位置      更新时间:2023-09-26

im使用此代码javascript与localStorage(类似于php中的会话)

<script>
$(function(){
        var a = localStorage.getItem('login');
        if(a=="" || a==null){window.location.href = "dash-home.html";}
        else{window.location.href = "dash-chat.html";}
    }); 
</script>

但是当定位在dash-home.html中时,该页面总是刷新页面(F5重复)

我想您已经明白AJAX/PHP是最好的解决方案。

但是,如果不能使用AJAX,可以使用.load()jQuery方法来模拟AJAX。.load()会将另一个文件的内容加载到您想要的文档WHEN和WHERE中。

示例:

if (condition == true){
    $('targetDIV').load('desired_page.html');
}

http://api.jquery.com/load/


关于AJAX的更多信息:

https://stackoverflow.com/a/17974843


例如:

<script>
    $(function(){
        var a = localStorage.getItem('login');
        if(a=="" || a==null){$('#targetDIV').load("dash-home.html");}
        else{$('#targetDIV').load("dash-chat.html");}
    }); 
</script>
<body>
    <div id="targetDIV"> This is where the other pages will appear </div>