Phonegap - 自动重定向到页面

时间:2017-09-06 11:42:48

标签: javascript jquery cordova mobile phonegap

我想使用Web堆栈和带有phonegap的包来开发移动应用程序。 我的index.html页面将包含一个登录表单。我有一个js函数ComboBox; 检查用户是否通过查找localStorage变量登录。

挑战:如果checkLoggedIn(),我希望index.html自动重定向到member.html 返回checkLoggedIn();否则它将不会重定向,只停留在true上,这意味着index.html将在checkLoggedIn()上运行。 我不知道要发射什么样的事件或者如何解雇它来实现这一目标。

2 个答案:

答案 0 :(得分:1)

怎么样:

if(checkLoggedIn()) window.location = "member.html";

答案 1 :(得分:0)

这是我自动登录功能的工作代码,希望这适用于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 handler 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")

    }
}

上面的代码可以正常工作,你需要维护一个会话

相关问题