JQuery's cookie plug in implementation saves user login information

  • 2020-03-30 02:38:12
  • OfStack

 
<!DOCTYPE html> 
<html> 
<head> 
<title>cookies.html</title> 

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="this is my page"> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

<style type="text/css"> 
.txt{ 
width: 150px; 
height:20px; 
border: 1px blue solid; 
border-radius:0.5em; 
margin-bottom: 5px; 
padding-left: 5px; 
} 

</style> 
<script type="text/javascript" src="../js/jquery-1.10.2.js"></script> 
<script type="text/javascript" src="../js/jquery.cookie.js"></script> 
<script type="text/javascript"> 
$(function(){ 


if($.cookie("name")){ 
//The value is assigned if it exists
$("#username").val($.cookie("name")); 

} 


$("#mycookie").submit(function(){ 

//If the save username option is selected
if($("#chkSave").is(":checked")){ 

//Set the Cookie value
$.cookie("name",$("#username").val(),{ 
expires:7,//Set a save period
path:"/"//Set the saved path

}); 

}else{ 

//Destruction of objects
$.cookie("name",null,{ 
path:"/" 

}); 
} 

return false; 
}); 

}); 


</script> 
</head> 
<body> 
<form action="#" method="get" id="mycookie"> 

<div> 
 User name: <br> 
<input id="username" name="username" type="text" class="txt"> 
</div> 
<div> 
 password :<br> 
<input id="password" name="password" type="password" class="txt"> 
</div> 
<div> 
<input id="chkSave" type="checkbox"> Whether to save the user name  
</div> 
<div> 
<input id="cookBtn" class="btn" type="submit" value=" submit "> 
</div> 

</form> 
</body> 
</html> 

Related articles: