如何在localStorage中的javascript窗口位置但不重新加载页面?

时间:2016-01-12 03:20:17

标签: javascript jquery html local-storage

即时通讯使用此代码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重复)

1 个答案:

答案 0 :(得分:0)

我认为您了解AJAX / PHP是最佳解决方案。

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

示例:

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>