简体   繁体   English

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

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

im using this code javascript with localStorage (something like session in php) 我将这段代码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>

but when location in dash-home.html, that page always refresh page (F5 repetitive) 但是当在dash-home.html中定位时,该页面始终刷新页面(F5重复)

I think you understand that AJAX / PHP is the best solution. 我认为您了解AJAX / PHP是最好的解决方案。

But if you cannot use AJAX, you can use the .load() jQuery method to simulate AJAX. 但是,如果您不能使用AJAX,则可以使用.load() jQuery方法来模拟 AJAX。 .load() will load the contents of another file into your document WHEN and WHERE you want. .load()会在需要的时间和位置将另一个文件的内容加载到您的文档中。

Example: 例:

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

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


More information about AJAX: 有关AJAX的更多信息:

https://stackoverflow.com/a/17974843 https://stackoverflow.com/a/17974843


For your example: 例如:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM