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>
LocalStorage. Local storage provides simple, synchronous key/value pair storage, and is supported by the underlying WebView implementations on all Cordova platforms.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With