クライアント側の Web アプリケーションの sessionStorage を使用しています。index.html に、jQuery を使用して sessionStorage 変数を設定します。
$.ajax({
type: "POST",
url: "login.php",
data: "user="+$("#user_name").val()+"&password="+$("#password").val(),
success: function(html){
if(html=='true') {
$("#add_err").html("Correct. Loading...")
sessionStorage.setItem('sessionUser',$("#user_name").val());
sessionStorage.setItem('sessionPassword',$("#password").val());
setTimeout(function(){
$(location).attr('href','eval.html');
},2000);
} else {
$("#add_err").html("Incorrect");
}
},
});
Ok。できます。eval.html で、アラート alert(sessionStorage.getItem('sessionUser')); を起動します。ユーザーを表示します。
アプリケーションを切断したい場合は、ボタン id=btnLogout をクリックします。
$("#btnLogout").click(function(){
sessionStorage.clear();
window.location.assign('index.html');
});
しかし、sessionStorage は何もクリアしません。別のアラートを index.html に配置しましたが、sessionUser は削除されません。
私は間違っていることを知りません。
index.html = login.js からのログイン ボタン:
$(document).ready(function(){
$("#login").click(function(){
$.ajax({
type: "POST",
url: "login.php",
data: "username="+$("#user_name").val()+"&password="+$("#password").val(),
success: function(html){
if(html=='true') {
$("#add_err").html("User correct. Loading application...")
sessionStorage.setItem('sessionUser',$("#user_name").val());
sessionStorage.setItem('sessionPassword',$("#password").val());
setTimeout(function(){
$(location).attr('href','eval.html');
},2000);
} else {
$("#add_err").html("Incorrect");
}
},
});
});
});
eval.html からのログアウト ボタン:
$(document).ready(function(){
$("#btnLogout").click(function(){
sessionStorage.removeItem('sessionUser');
sessionStorage.removeItem('sessionPassword');
window.location.assign('index.html');
});
});