2013年2月28日木曜日

開発環境

『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の10章(クッキーとその後継技術)練習問第10-1.を解いてみる。

その他参考書籍

10-1.

コード(BBEdit)

var result = "",
    setCookie = function ( key, value ) {
        var cookieDate = new Date(2030, 12, 31);
        document.cookie = key + "=" + escape( value ) + "; expires="
            + cookieDate.toUTCString() + "; path=/";
    },
    readCookie = function ( key ) {
        var cookie = document.cookie,
            first = cookie.indexOf(key+"="),
            s, last;
        if (first >= 0) {
            s = cookie.substring(first, cookie.length);
            last = s.indexOf(";");
            if (last < 0) {
                last = s.length;
            }
            s = s.substring(0, last).split("=");
            return unescape(s[1]);
        }
        return null;
    },
    key = "visited";
if (navigator.cookieEnabled) {
    if (readCookie(key)) {
        result = "こんにちは";
    } else {
        result = "はじめまして";
        setCookie(key, 1);
    }
} else {
    result = "このページはクッキーの利用を前提としております。" +
        "ブラウザの設定を変更してからご利用ください。";
}
$('#pre0').text(result);











						

0 コメント:

コメントを投稿