1

So I am trying use getLocalData() and setLocalData() in my Smartbanner. But everytime it tells me that it is not defined. How come? I cant seem to figure out why. I am not sure if I have to insert them into the init, or it may be due to the use of 'this', since I am not 100% sure if I am using it right. However, When I take out getLocalData(), setLocalData() can be executed and it works. So apperantly, only getlocalData() doesn't work.

var Varova_LocalStorageManager = function ( id, options ) {
  var siteID = null;

  this.init = function () {
    if(typeof options != "undefined" && "siteID" in options){
      siteID = options.siteID;
    } else {
      console.log(id + " error","No cookiename specified, did you pass in the options object?");
    }
  };
  function getOrSetData(operation, key, value){
    var itemKey = siteID + "_" + key;
    if (typeof localStorage != "undefined") {
      if(operation == "set"){
        localStorage.setItem(itemKey, value)
      } else {
        localStorage.getItem(itemKey)
      }
    } else {
      if(operation == "set"){
        setCookie(itemKey, value);
      } else {
        getCookie(itemKey);
      }
    }
  };
  this.getLocalData = function(key, value) {
    getOrSetData('get',key,value);
  };
  this.setLocalData = function (key, value) {
    getOrSetData('set',key,value);
  };
  return this;
};

    Varova_LocalStoreManager();


var Varova_mobileAppSmartbanner = function (id, options) {
    var smartBanner             = document.getElementById('smartbanner');
    var banner_height       = smartBanner.offsetHeight;
    var localStorageName    = getLocalData('app_installed');


    this.init = function(){
        addEventListeners();
        document.getElementsByClassName('smartbanner-button')[0].setAttribute('action', getAppBannerUrl());
        showOrHideSmartbanner();
    };

    var getAppBannerUrl = function() {
        var url = "";
        if(options && "urls" in options && "iosAppUrl" in options.urls && "androidAppUrl" in options.urls){
            var iphone  = navigator.userAgent.toLowerCase().indexOf("iphone") > -1;
            var android = navigator.userAgent.toLowerCase().indexOf("android") > -1;

            if (iphone) {
                url = options.urls.iosAppUrl;
            }
            if (android) {
                url = options.urls.androidAppUrl;
            }
        } else {
            console.log("Required parameters not available");
        }
        return url;
    };

    var addEventListeners = function() {
        document.querySelector('.smartbanner-input, .smartbanner-close').addEventListener('click', hideBanner);

        function hideBanner() {
            smartBanner.style.display = 'none';
            // setLocalData('app_installed', 'true')
            document.querySelector('.header').style.cssText += 'margin-top: 0; transition-property: margin-top; transition-duration: 200ms';
        }
    };

    var showOrHideSmartbanner = function (){
        var chrome_ios      = navigator.userAgent.match('CriOS');
        var is_safari       = navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && !chrome_ios;
        var notMobileDevice = screen.width > 1024;

        if (localStorageName == "true" || notMobileDevice) {
            smartBanner.style.display = 'none';
         } else if (is_safari) {
             document.getElementsByTagName('head')[0].appendChild( '<meta name="apple-itunes-app" content="app-id=886151323">' );
             smartBanner.style.display = 'none';
        } else {
                smartBanner.style.top = '0';
                document.querySelector('.header').style.cssText += 'margin-top:' + banner_height.toString() + 'px; transition: margin-top 300ms;';
        }
    };
    return this;
};
S.Reyes
  • 81
  • 1
  • 6
  • Maybe not a dup, but related: http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Teemu Mar 15 '17 at 10:27

0 Answers0