Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 localstorage in Cordova

Does HTML5 Local Storage work in Cordova / PhoneGap? I am trying to use it, both the HTML5 way and the way specified in the docs. Neither work.

Specifically, I am trying to use an ajax query result for local storage. I have tested the query, and it works.

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
    <title>Hello World</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript">

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("form").submit(function () {

                var uname = document.getElementById("username").value;
                var pword = document.getElementById("password").value;
                var postData = {
                    username: uname,
                    password: pword
                };

                $.ajax({
                    url: "http://www.yellowcabsavannah.com/test.php",
                    type: "POST",
                    data: postData,
                    async: false,
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        localStorage.uname = data.username;
                        localStorage.pword = data.password;
                        alert(localStorage.uname);
                    }
                }
                });
            return false;
            });
        });
    </script>
</head>

<body>
    <form action="">
        <input type='text' id="username" name="username" placeholder="Username">
        <br>
        <input type='password' id="password" name="password" placeholder="password">
        <br>
        <input type="submit" id="submit" value="Login">
    </form>
</body>

like image 290
copilot0910 Avatar asked Feb 11 '13 01:02

copilot0910


People also ask

Does LocalStorage work in Cordova?

LocalStorage. Local storage provides simple, synchronous key/value pair storage, and is supported by the underlying WebView implementations on all Cordova platforms.

What is HTML5 LocalStorage?

HTML5 local storage is a component of the Web storage application programming interface. It is a method by which Web pages locally store named key/value pairs inside a client's Web browser.

Can I use LocalStorage instead of cookies?

Cookies and local storage serve different purposes. Cookies are mainly for reading server-side, whereas local storage can only be read by the client-side . Apart from saving data, a big technical difference is the size of data you can store, and as I mentioned earlier localStorage gives you more to work with.

Can we store JSON in LocalStorage?

Storing JSON, Functions And Arrays in localStorage Luckily, we can store complex data in localStorage by leveraging the JSON object's stringify() and parse() functions. JSON support is quite good on mobile but if you need to add support use json2.


1 Answers

I've used local storage like this:

// To store a value
window.localStorage.setItem('key', value);

// To retrieve a value
value = window.localStorage.getItem('key');

// To delete a storage
window.localStorage.removeItem('key');

Hope that helps.

like image 155
SHANK Avatar answered Oct 06 '22 12:10

SHANK