【发布时间】:2018-02-15 20:19:28
【问题描述】:
我有一个应用程序,您可以在启动时选择 - 您是贡献者还是用户。之后,我希望始终为贡献者或用户加载起始页面。我知道您可以将<content src="index.html" /> 设置为在启动时执行一次,但我怎样才能动态执行呢?
【问题讨论】:
标签: mobile phonegap html-framework-7
我有一个应用程序,您可以在启动时选择 - 您是贡献者还是用户。之后,我希望始终为贡献者或用户加载起始页面。我知道您可以将<content src="index.html" /> 设置为在启动时执行一次,但我怎样才能动态执行呢?
【问题讨论】:
标签: mobile phonegap html-framework-7
@proofzy 的答案是正确的,但你仍然可以只使用 DOM7 而不是 Jquery 来做到这一点
在你的 JS 文件中:
//to save data if user pick contributor or user button after first start.
$$("#contributor").on('click', function(){
localStorage.setItem("whois", "contributor");
});
//And call this same script but for user:
$$("#user").on('click', function(){
localStorage.setItem("whois", "user");
});
//So call this function on the end of page index.html and it will redirect programmatically
function check(){
if (localStorage.getItem("whois") !== null) { //if whois exists on localStorage
if (localStorage.getItem("whois") == "user"){ // if is USER send to User Page
window.location.href = "userIndex.html"
}else if (localStorage.getItem("whois") == "contributor"){ // if is USER send to contributor Page
window.location.href = "contributorIndex.html"
}
}
}
还有很多其他方法可以做到这一点,甚至更好,但这是最简单的。
【讨论】:
你必须使用
本地存储
如果用户在第一次启动后选择贡献者或用户按钮,则保存数据。 简单使用jQuery脚本:
<script>
$("#contributor").click( function()
{
//alert('button clicked');
localStorage.setItem("contributor", "contributor");
}
);
</script>
并为用户调用相同的脚本:
<script>
$("#user").click( function()
{
//alert('button clicked');
localStorage.setItem("user", "user");
}
);
</script>
如果用户之前按“用户”或“贡献者”,则在下一个 html 页面控制。
$(document).ready(function() {
if (localStorage.getItem("user") === null) {
//user is null
} else {
document.location.href = "userIndex.html"
}
if (localStorage.getItem("contributor") === null) {
//contributor is null
} else {
document.location.href = "contributorIndex.html"
}
});
祝你好运!
【讨论】: