Skip to content

Instantly share code, notes, and snippets.

@Himenon
Last active September 11, 2015 11:40
Show Gist options
  • Save Himenon/c1dd28dc8b9022c3b16b to your computer and use it in GitHub Desktop.
Save Himenon/c1dd28dc8b9022c3b16b to your computer and use it in GitHub Desktop.
$(function() {
// 初めての場合は undefined が代入される
var localCount = localStorage.count;
console.log("localCount = " + localCount);
if (!localCount) {
// undefinedが格納された場合の処理
$("#check").click(function() {
// #checkをクリック時にポストする
$.post('/new', {
name: 'value1'
}, function(data, textStatus, xhr) {
console.log("textStatus = " + textStatus);
if (textStatus == "success") {
// postが成功した場合はカウンターを1にする
localStorage.count = 1;
};
});
});
} else {
// 投稿した場合はカウンターが1なので、submitできないように要素を隠す
$("#check").hidden();
}
});
@Himenon
Copy link
Author

Himenon commented Sep 11, 2015

HTMLの例です

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Storage Check</title>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="submitOnlyOne.js"></script>
</head>
<body>
    <h1>WEB STORAGEを使った投稿チェック</h1>
    <button id="check">投稿する</button>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment