Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript issue in IE12 or lower #359

Open
TemitaTom opened this issue Nov 8, 2017 · 2 comments
Open

JavaScript issue in IE12 or lower #359

TemitaTom opened this issue Nov 8, 2017 · 2 comments

Comments

@TemitaTom
Copy link

TemitaTom commented Nov 8, 2017

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

@menzow
Copy link

menzow commented Nov 15, 2017

IE supports try..catch..finally since ie6. I think the issue lies within your control flow that handles the result of addToLocalStorage. Could you share your implementation?

I confirmed compatibility with ie9 and ie10 using this fiddle: https://jsfiddle.net/x78novpf/2/.

@TemitaTom
Copy link
Author

Hi, yes, try..finally works well but in this case fails inside http calback function only. And some functions with try...finally not all parts return data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants