1. 程式人生 > 其它 >(十一)Beego模型之SQL查詢

(十一)Beego模型之SQL查詢

今天寫了一個demo,發現無法觸發window.addEventListener('storage',()=>{});

查了一下資料,發現這是其他標籤頁面觸發。

只能怪自己看書籍不夠細心。

<!DOCTYPE html>
<html>

<head>
    <title>利用storage事件實時監視Web Storage中的資料</title>
    <script>
        window.addEventListener('storage', function (event) {
            if (event.key == "test") {
                var output = document.getElementById("output");
                output.innerHTML = "原有值:" + event.oldValue;
                output.innerHTML += "<br/>新值:" + event.newValue;
                output.innerHTML += "<br/>變動頁面地址:" +
                    utf8_decode(unescape(event.url));
                console.log(event.storageArea);

                // 此行程式碼只在Chrome瀏覽器中有效
                console.log(event.storageArea === localStorage); // 輸出true 
            }
        }, false);
        function utf8_decode(utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while (i < utftext.length) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                }
                else if ((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i + 1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                }
                else {
                    c2 = utftext.charCodeAt(i + 1);
                    c3 = utftext.charCodeAt(i + 2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6)
                        | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }  
    </script>
</head>
<script>
    function setLocalStorage() {
        localStorage.test = document.getElementById("text1").value;
    } 
</script>

<body>
    請輸入一些值:<input type="text" id="text1" />
    <button onClick="setLocalStorage()">設定</button>
    <output id="output"></div>
</body>

</html>