JS Code Snippet --- Cookie
<a id="quitBtn" href="#" class="exit">Exit</a>
<a id="quitBtn" href="" class="exit">Exit</a>
$(function(){
$('#loginBtn').on('click',loginBtnClick);
if(pageRefresh()){
$('#login').hide();
$('#home').show();
}else{
$('#login').show();
$('#home').hide();
}
});
/**
* @brief To check pin and token, called when use F5 to refresh.
* @return if pin and token both exists return true, other false
*/
function pageRefresh()
{
var pin = getCookie(APP.pinName);
var token = getCookie(APP.tokenName);
if( !pin || typeof(pin) == 'undefined' || pin.length == 0 ){
deleteCookie(APP.pinName); deleteCookie(APP.tokenName);
return false;
}
if( !token || typeof(token) == 'undefined' || token.length == 0 ){
deleteCookie(APP.pinName); deleteCookie(APP.tokenName);
return false;
}
/// if run here, means pin and token exist.
var form = new FormData();
form.append( 'pin', pin );
form.append( 'token', token );
var ajaxResult = false;
$.ajax({
url: 'POST_PAGE_REFRESH' ,
type: 'POST',
data: form,
async: false,
cache: false,
dataType:'json',
contentType: false,
processData: false,
success:function( data ){
if( 'undefined' == typeof(data.state) ){
alert('data.state undefined');
ajaxResult = false;
return;
}
if( typeof(data.msg) == 'undefined' ){
ajaxResult = false;
alert('data.msg undefined');
return;
}
if( 'ok' !== data.state )
{
deleteCookie(APP.pinName); deleteCookie(APP.tokenName);
ajaxResult = false;
alert("Error 1 :" + data.msg);
return;
}
ajaxResult = true;
},
error:function(xhr){
ajaxResult = false;
return;
}
});
return ajaxResult;
}
/**
* @beief PIN Code login click button
*/
function loginBtnClick()
{
var pin = $('input[name="pin"]').val();
pin.trim();
if( pin == "" ){
alert("PIN Code Not Empty.");
$('input[name="pin"]').val("");
return;
}
if( pin.length !== 4 ){
alert("The length of Input PIN Code must be 4 number char.");
$('input[name="pin"]').val("");
return;
}
var token = randomString(32);
var form = new FormData();
form.append( 'pin', pin );
form.append( 'token', token );
$.ajax({
url: 'POST_PIN_TOKEN' ,
type: 'POST',
data: form,
async: true,
cache: false,
dataType:'json',
contentType: false,
processData: false,
success:function( data ){
if( !checkAjaxData(data) ){
$('input[name="pin"]').val("");
return;
}
addCookie(APP.pinName, pin, 1);
addCookie(APP.tokenName, token, 1);
$('#login').hide();
$('#home').show();
},
error:function(xhr){
console.error( "Error 2: " + xhr.status + " " + xhr.statusText );
}
});
}
$( function(){
$('#quitBtn').on('click', quitBtnClick);
}
/**
* @biref: Quit Button Click Function
* and them tell server to delete token
*/
function quitBtnClick()
{
var quitForm = new FormData();
quitForm.append('pin',getCookie(APP.pinName));
quitForm.append('token',getCookie(APP.tokenName));
$.ajax({
url: 'POST_QUIT',
type: 'POST',
data: quitForm,
async: true,
cache: false,
dataType:'json',
contentType: false,
processData: false,
success: function (data) {
if( !checkAjaxData(data) ){
return;
}
deleteCookie(APP.pinName); deleteCookie(APP.tokenName);
location.href = "./";
},
error: function (data) {
//console.log('load Error: '+xhr.status+': '+xhr.statusText);
}
});
}
JS Code Snippet --- Cookie的更多相关文章
- .NET获取不到js写的cookie解决方法
今晚使用javascript设置一个来路的cookie,之后使用ASP.NET获取这个cookie值,发现ASP.NET获取不到JS设置的cookie值,真郁闷中,以下是JS写Cookie的代码: C ...
- CKEditor 4.4.1 添加代码高亮显示插件功能--使用官方推荐Code Snippet插件
随着CKEditor4.4.1的发布,以前一直困扰的代码高亮问题终于完美的得到解决,在CKEditor4.4中官方发布了Code Snippet这个代码片段的插件,终于可以完美的内嵌使用代码高亮了,以 ...
- 使用 Code Snippet 简化 Coding
在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符一个字符敲还是使用 Visual Studio 提 ...
- js简单操作Cookie
贴一段js简单操作Cookie的代码: //获取指定名称的cookie的值 function getCookie(objName) { var arrStr = document.cookie.spl ...
- Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!
使用Code Snippet简化Coding 在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符 ...
- 如何创建 Code Snippet
比如有一行自定义代码段: @property (nonatomic,copy) NSString *<#string#>; 需要添加到 Code Snippet 上,以帮助开发人员开发更便 ...
- ES6 will change the way you write JS code.
https://hacks.mozilla.org/2015/04/es6-in-depth-an-introduction/ Counting to 6 The previous editions ...
- 善用VS中的Code Snippet来提高开发效率
http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...
- 介绍 .Net工具Code Snippet 与 Sql Server2008工具SSMS Tools Pack
不久前,某某在微软写了一个很酷的工具:Visual Stuido2008可视化代码片断工具,这个工具可以在http://www.codeplex.com/SnippetDesigner上免费下载,用它 ...
随机推荐
- DataReader使用
一.DataReader含义 DataReader相比于DataSet,DataReader是一个抽象类,所以不能用DataReader DR = new DataReader(),来构造函数创建对象 ...
- linux命令返回值的含义
linux命令执行后无论成功与否都有一个返回值: 如果为 0,则表示命令执行成功,其它值则表示错误, 具体的错误码含义如下: "OS error code 1: Operation not ...
- c++ int 转 string 实现前缀补0
最近需要读取一些格式化路径的文件.路径文件名字最后是数字从0开始到100结束,但是占了4位,即0000到0100这样. 需要将int转成string的同时加上前缀0. 方法一: 用sprintf()函 ...
- CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)
You are a given a list of integers a 1 ,a 2 ,…,a n a1,a2,…,an and s s of its segments [l j ;r j ] [ ...
- threejs精灵平面Sprite(类似tip效果)
效果图: let center = this.cube.position.clone(), size = this.cube.geometry.boundingBox.getSize(), sca ...
- opencv之读取pts文件
#include <iostream> #include <fstream> #include <opencv2/opencv.hpp> using namespa ...
- PhoneGap 获得APP的VersionName
1.首先安装cordova-plugin-app-version cordova plugin add cordova-plugin-app-version 2. 调用方法如下 function ge ...
- VBA7种文档遍历法
Sub 在选定文档最后加入一句话() '遍历文件 Dim MyDialog As FileDialog On Error Resume Next Application.ScreenUpdating ...
- <meta name="viewport" content="width=device-width, initial-scale=1.0">的说明
今天在做适配手机版时,chrome调到手机版,但是还是显示PC端的样式,无法展现出手机端的样式: 开始的时候还以为是chrome版本的问题,最新版本的chrome62.0是有很多变化的,而之前工作中使 ...
- mysql connection phase(未整理)
14.2.1 初始握手初始握手从服务器发送 Initial_Handshake_Packet开始.在这之后,客户端可以选择是否通过SSL_Connection_Request_Packet发送SSL连 ...