1

I have a localStrg.js file for 5 html pages and within pages there're elements that doesn't exist in some pages. I want to verify if such element exists and if it does get Its value and use it as default/ I have this in the js file:

 function initialize() {
                // Test to see if we support the Storage API
                var SupportsLocal = (('localStorage' in window) && window['localStorage'] !== null);
                var SupportsSession = (('sessionStorage' in window) && window['sessionStorage'] !== null);

                // if either one is not supported, then bail on the demo
                if (!SupportsLocal || !SupportsSession) {
                    document.getElementById('infoform').innerHTML = "<p>Sorry, this browser does not support the W3C Storage API.</p>";
                    return;
                }
                // if the localStorage object has some content, restore it
                if (window.localStorage.length != 0) {

                        for(i=0;i<window.localStorage.length;i++){
                            if(document.getElementById(window.localStorage.key(i))==!null){
                            alert("entered for");
                                getLocalContent(window.localStorage.key(i));
                                alert("got local content");
                                if(document.getElementById(window.localStorage.key(i)).type == "checkbox"){
                                    alert("if");
                                    document.getElementById(window.localStorage.key(i)).checked = true;

                                }
                            alert("out of if");
                        }
                }
                        alert("out of for");

                }
 }

 function storeLocalContent(elementId,value){
    window.localStorage.setItem(elementId,value);   
 }

 function getLocalContent(elementId){
     document.getElementById(elementId).value = window.localStorage.getItem(elementId);
 }

 function clear(){
     window.localStorage.clear();
 }

 function removeItem(elementId){
     window.localStorage.removeItem(elementId);
 }

 function isChecked(elementId, value){
     if(document.getElementById(elementId).checked == false){
        removeItem(elementId);
     }
     else if(document.getElementById(elementId).checked === true){
         storeLocalContent(elementId, value);
     }
 }


 window.onload = function(){
     initialize();

 }

but everytime i get "out of for"

4

1 回答 1

1

此行中的比较运算符是错误的:

if(document.getElementById(window.localStorage.key(i))==!null){

它应该是

if(document.getElementById(window.localStorage.key(i))!==null){
于 2013-07-22T14:54:52.573 回答