如何使用WebStorage不显示

时间:2019-07-31 02:03:52

标签: javascript css wordpress

嗨,如果引荐来源网址为example.com,我想做的就是为我正在使用网络存储但无法正常工作的所有页面隐藏一些数据

<script>


if (document.referrer !== 'example.com/') {
// Store
localStorage.setItem('displaynone', ' <style> 

.top-bar{display:none!important;}
.woocommerce-breadcrumb{display:none!important;}
.register-benefits{display:none!important;}
.before-login-text{display:none!important;}
#colophon{display:none!important;} </</style>');
}

localStorage.getItem('displaynone');




</script>

谁能告诉我怎么了?

1 个答案:

答案 0 :(得分:0)

您好,这段代码将帮助您使用webstorage.js

    var cookieStoragePlan = {
        create: function(name, value, path, domain, expires){
    if(!name)
        return null;
    var cookie = (name) + '=';

    var time = new Date();
    time.setTime((new Date().getTime())+1000*60*60*24);

    if(typeof expires !== 'undefined'){
        time.setTime((new Date().getTime())+expires*1000);
    }

    cookie += typeof value === 'undefined' ? '' : encodeURIComponent(value);
    cookie += typeof path === 'undefined' ? '' : '; path=' + (path);
    //cookie += typeof domain === 'undefined' ? '; domain=' +       

    encodeURIComponent(document.domain)  : '; domain=' + encodeURIComponent(domain);
    cookie += '; expires=' + time.toUTCString();

    document.cookie = cookie;
    //console.log(document.cookie);
},

getValue: function(name){
    if(!name){
        return null;
    }
    var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
    var cookie = decodeURIComponent(document.cookie);
    var result = cookie.match(reg);
    return (!result ? null : result[2]);
},

isPresent: function(name){
    if(!name){
        return false;
    }
    var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
    return reg.test(decodeURIComponent(document.cookie));
},

remove: function(name){
    if(!name || this.isPresent(name)===false){
        return null;    
    }
    var time = new Date();
    time.setTime(0);
    var cookie = name + '=; expires=' + time.toUTCString(); 
    document.cookie = cookie;
},

clear: function(){
    var allCookie = decodeURIComponent(document.cookie);
    var row = allCookie.split('; ');
    for(var i = 0; i < row.length; ++i){
        var pos = row[i].indexOf('=');
        var key = row[i].substring(0, pos);
        this.remove(key);
    }
}
    };

    var userDataPlan = {
storage: null,

init: function(expires){
    if(this.storage !== null){
        if(expires!=='undefined'){
            var now = new Date().getTime();
            var time = now + 1000*60*60*24*expires;
            this.storage.expires = new Date(time).toUTCString();
        }   
        return true;
    }
    try{        
        this.storage = document.createElement('div');
        //this.storage.id = 'userDataPlan';
        this.storage.style.display = 'none';
        this.storage.addBehavior('#default#userData');
        document.body.appendChild(this.storage);
        if(expires!=='undefined'){
            var now = new Date().getTime();
            var time = now + 1000*60*60*24*expires;
            this.storage.expires = new Date(time).toUTCString();
        }
    }
    catch(e){
        return false;
    }
    return true;
        },

        create: function(name, value, expires){

    if(!this.init(expires) || !name)
        return false;
    try{    
        this.storage.load('myUserDataPlan');
        this.storage.setAttribute(name, value || null);

        this.storage.save('myUserDataPlan');
    }
    catch(e){
        return false;
    }
    return true;
},

getValue: function(name){
    if(!this.init() || !name){
        return null;
    }
    this.storage.load('myUserDataPlan');
    return this.storage.getAttribute(name) || null;
},

isPresent: function(name){

    if(!this.init() || !this.getValue(name))
        return false;
    return true;
},

remove: function(name){
    if(!this.isPresent(name))
        return true;
    this.storage.removeAttribute(name);
    try{
        storage.save('myUserDataPlan');
    }
    catch(e){
        return false;
    }
    return false;
},

clear: function(){
    if(!this.init(-1)){
        return false;
    }
    storage.parentNode.removeChild(storage);
    return true;
}
    };

    var localStoragePlan = {
create: function(name, value){
    localStorage.setItem(name, value);
},

getValue: function(name){
    return localStorage.getItem(name);
},

isPresent: function(name){
    if(this.getValue(name))
        return true;
    return false;
},

remove: function(name){
    localStorage.removeItem(name);
},

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

    var StoragePlan = function(){};

    StoragePlan.prototype = {
handler: function(){
    return cookieStoragePlan;
    if(typeof window.localStorage === 'object'){
        //alert("localStoragePlan");
        return localStoragePlan;
    }
    else if(document.body.addBehavior()){
        //alert("userDataPlan");
        return userDataPlan;
    }
    else {
        //alert("cookieStoragePlan");
        return cookieStoragePlan;
    }
  }
      };