Skip to content

JavaScript issue in IE12 or lower #359

@TemitaTom

Description

@TemitaTom

Hi, some errors in javascript

Versions:

  • Angular: 1.6.6
  • Angular Local Storage: 0.7.1
  • Browser: Internet Explorer 11

Case:
Inside callback function, localStorage plugin call "finally" clause after try catch. This not work in IE 12-
For solve this, please use try catch correcly -if is possible- and return in every function a default object if it is fail.

Example:

Current code

// Directly adds a value to local storage
// If local storage is not available in the browser use cookies
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value, type) {
var previousType = getStorageType();

    try {
      setStorageType(type);

      // Let's convert undefined values to null to get the value consistent
      if (isUndefined(value)) {
        value = null;
      } else {
        value = toJson(value);
      }

      // If this browser does not support local storage use cookies
      if (!browserSupportsLocalStorage && self.defaultToCookie || self.storageType === 'cookie') {
        if (!browserSupportsLocalStorage) {
          $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
        }

        if (notify.setItem) {
          $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
        }
        return addToCookies(key, value);
      }

      try {
        if (webStorage) {
          webStorage.setItem(deriveQualifiedKey(key), value);
        }
        if (notify.setItem) {
          $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
        }
      } catch (e) {
        $rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
        return addToCookies(key, value);
      }
      return true;
    } finally {
      setStorageType(previousType);
    }
  };

Working code

// Directly adds a value to local storage
// If local storage is not available in the browser use cookies
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value, type) {

  var previousType = getStorageType();
  var result = false;

  try {
  
  	setStorageType(type);

  	// Let's convert undefined values to null to get the value consistent
  	if (isUndefined(value)) {
  		value = null;
  	} 
  	else {
  		value = toJson(value);
  	}

  	// If this browser does not support local storage use cookies
  	if (!browserSupportsLocalStorage && self.defaultToCookie || self.storageType === 'cookie') {
  		if (!browserSupportsLocalStorage) {
  			$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
  		}

  		if (notify.setItem) {
  			$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
  		}
  		
  		result = addToCookies(key, value);
  	}
  	else {
  		try {
  			
  			if (webStorage) {
  				webStorage.setItem(deriveQualifiedKey(key), value);
  			}
  			if (notify.setItem) {
  				$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
  			}
  			result = true;
  		} 
  		catch (e) {
  			
  			$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
  			result = addToCookies(key, value);
  		}
  	}
  }
  catch (e) { 
  	result = false;
  }
  finally { 
  	setStorageType(previousType);
  }
  
  return result;

};

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions