Phonegap - 自动重定向到一个页面
Phonegap - Automatically redirecting to a page
我想使用 Web 堆栈开发移动应用程序并使用 phonegap 进行打包。
我的 index.html 页面将包含一个登录表单。我有一个 js 函数 checkLoggedIn()
;
通过查找 localStorage 变量检查用户是否登录。
挑战 : 我希望 index.html
如果 checkLoggedIn()
自动重定向到 member.html
returns true
;否则它不会重定向,只会停留在 index.html
,这意味着 checkLoggedIn()
将在 index.html
加载时 运行。
我不知道要触发什么样的事件或如何触发它来实现这一点。
怎么样:
if(checkLoggedIn()) window.location = "member.html";
这是我的自动登录功能的工作代码希望它适用于 android 和 ios
下面是输入用户名和密码后登录按钮的代码
$('#login').click(function () {
var userName = $('#Username').val();
var password = $('#password').val();
if (userName == "" || password == "") {
window.plugins.toast.showLongBottom("Please enter a valid data");
return false;
}
var options = { dimBackground: true };
SpinnerPlugin.activityStart("Loading...", options);
$.ajax({
type: 'GET',
url: xxxxxx.xxxx.xxxxx,
data: "uid=" + userName + "&pwd=" + password + "",
success: function (resp) {
SpinnerPlugin.activityStop();
if (resp.status != 0) {
if (resp.RoleId == 1) {
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
window.open('Add.html', '_self', 'location=no');
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() mash.Settings.sessionTimeoutInMSec);
}
else {
SpinnerPlugin.activityStop();
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
var username = userName;
var password = password;
window.localStorage.setItem("uname", resp.Name);
window.localStorage.setItem("pass", password);
window.localStorage.setItem("sno", resp.sno);
window.localStorage.setItem("RoleId", resp.RoleId);
window.open('Main.html', '_self', 'location=no');
//window.plugins.toast.showLongBottom("Login Successfull");
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() + mash.Settings.sessionTimeoutInMSec);
}
}
else {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Please Enter valid Username and password");
SpinnerPlugin.activityStop();
}
},
error: function (e) {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Invalid Data");
SpinnerPlugin.activityStop();
}
});
});
在此之后 index.html 使用 onload 处理程序 fillpassword() 来使用下面的函数
function fillpassword() {
if (window.localStorage.getItem("uname") != 0) {
mash.Session.getInstance().set({
userId: window.localStorage.getItem("sno"),
userName: window.localStorage.getItem("uname"),
});
if (window.localStorage.getItem("RoleId") != 1) {
document.getElementById('Username').value = window.localStorage.getItem("uname");
document.getElementById('password').value = window.localStorage.getItem("pass");
window.open('Main.html', '_self', 'location=no');
}
}
else {
//alert("Yes")
}
}
以上代码有效,你只需要维护一个会话
我想使用 Web 堆栈开发移动应用程序并使用 phonegap 进行打包。
我的 index.html 页面将包含一个登录表单。我有一个 js 函数 checkLoggedIn()
;
通过查找 localStorage 变量检查用户是否登录。
挑战 : 我希望 index.html
如果 checkLoggedIn()
自动重定向到 member.html
returns true
;否则它不会重定向,只会停留在 index.html
,这意味着 checkLoggedIn()
将在 index.html
加载时 运行。
我不知道要触发什么样的事件或如何触发它来实现这一点。
怎么样:
if(checkLoggedIn()) window.location = "member.html";
这是我的自动登录功能的工作代码希望它适用于 android 和 ios
下面是输入用户名和密码后登录按钮的代码
$('#login').click(function () {
var userName = $('#Username').val();
var password = $('#password').val();
if (userName == "" || password == "") {
window.plugins.toast.showLongBottom("Please enter a valid data");
return false;
}
var options = { dimBackground: true };
SpinnerPlugin.activityStart("Loading...", options);
$.ajax({
type: 'GET',
url: xxxxxx.xxxx.xxxxx,
data: "uid=" + userName + "&pwd=" + password + "",
success: function (resp) {
SpinnerPlugin.activityStop();
if (resp.status != 0) {
if (resp.RoleId == 1) {
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
window.open('Add.html', '_self', 'location=no');
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() mash.Settings.sessionTimeoutInMSec);
}
else {
SpinnerPlugin.activityStop();
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
var username = userName;
var password = password;
window.localStorage.setItem("uname", resp.Name);
window.localStorage.setItem("pass", password);
window.localStorage.setItem("sno", resp.sno);
window.localStorage.setItem("RoleId", resp.RoleId);
window.open('Main.html', '_self', 'location=no');
//window.plugins.toast.showLongBottom("Login Successfull");
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() + mash.Settings.sessionTimeoutInMSec);
}
}
else {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Please Enter valid Username and password");
SpinnerPlugin.activityStop();
}
},
error: function (e) {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Invalid Data");
SpinnerPlugin.activityStop();
}
});
});
在此之后 index.html 使用 onload 处理程序 fillpassword() 来使用下面的函数
function fillpassword() {
if (window.localStorage.getItem("uname") != 0) {
mash.Session.getInstance().set({
userId: window.localStorage.getItem("sno"),
userName: window.localStorage.getItem("uname"),
});
if (window.localStorage.getItem("RoleId") != 1) {
document.getElementById('Username').value = window.localStorage.getItem("uname");
document.getElementById('password').value = window.localStorage.getItem("pass");
window.open('Main.html', '_self', 'location=no');
}
}
else {
//alert("Yes")
}
}
以上代码有效,你只需要维护一个会话