fixed issue with localstorage in ff with cookies disabled

firefox with cookies disabled panics and stops execution of the script
when checking for a localstorage-variable. added safer checking for
localstorage.
This commit is contained in:
Hannes Mannerheim 2013-08-19 21:15:15 +02:00
parent d277ed037f
commit b491d19dc9
3 changed files with 88 additions and 39 deletions

View File

@ -651,14 +651,16 @@ window.l.ar.adminCount = 'الإداريين';
// set language, from local storage, else browser language, else english
// set language, from local storage, else browser language, else english (english also if no localstorage availible)
var browserLang = navigator.language || navigator.userLanguage;
var selectedLanguage = 'en';
if(typeof localStorage.selectedLanguage != 'undefined') {
selectedLanguage = localStorage.selectedLanguage;
}
else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') {
selectedLanguage = browserLang.substring(0,2);
if(localStorageIsEnabled()) {
if(typeof localStorage.selectedLanguage != 'undefined') {
selectedLanguage = localStorage.selectedLanguage;
}
else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') {
selectedLanguage = browserLang.substring(0,2);
}
}
window.sL = window.l[selectedLanguage];

View File

@ -34,6 +34,28 @@
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
/* ·
·
· Checks if localstorage is availible
·
· We can't just do if(typeof localStorage.selectedLanguage != 'undefined')
· because firefox with cookies disabled then freaks out and stops executing js completely
·
· · · · · · · · · */
function localStorageIsEnabled() {
var mod = 'test';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
}
catch(e) {
return false;
}
}
/* ·
·
@ -233,24 +255,26 @@ function placeCaretAtEnd(el) {
· · · · · · · · · · · · · */
function updateHistoryLocalStorage() {
var i=0;
var localStorageName = window.username + '-history-container';
var historyContainer = new Object();
$.each($('#history-container .stream-selection'), function(key,obj) {
historyContainer[i] = new Object();
historyContainer[i].dataStreamName = $(obj).attr('data-stream-name');
historyContainer[i].dataStreamHeader = $(obj).attr('data-stream-header');
i++;
});
localStorage[localStorageName] = JSON.stringify(historyContainer);
if($('#history-container .stream-selection').length==0) {
$('#history-container').css('display','none');
if(localStorageIsEnabled()) {
var i=0;
var localStorageName = window.username + '-history-container';
var historyContainer = new Object();
$.each($('#history-container .stream-selection'), function(key,obj) {
historyContainer[i] = new Object();
historyContainer[i].dataStreamName = $(obj).attr('data-stream-name');
historyContainer[i].dataStreamHeader = $(obj).attr('data-stream-header');
i++;
});
localStorage[localStorageName] = JSON.stringify(historyContainer);
if($('#history-container .stream-selection').length==0) {
$('#history-container').css('display','none');
}
else {
$('#history-container').css('display','block');
}
$('#history-container').sortable({delay: 100});
$('#history-container').disableSelection();
}
else {
$('#history-container').css('display','block');
}
$('#history-container').sortable({delay: 100});
$('#history-container').disableSelection();
}
@ -261,16 +285,18 @@ function updateHistoryLocalStorage() {
· · · · · · · · · · · · · */
function loadHistoryFromLocalStorage() {
var localStorageName = window.username + '-history-container';
if(typeof localStorage[localStorageName] != "undefined") {
$('#history-container').css('display','block');
$('#history-container').html('');
var historyContainer = $.parseJSON(localStorage[localStorageName]);
$.each(historyContainer, function(key,obj) {
$('#history-container').append('<div class="stream-selection" data-stream-header="' + obj.dataStreamHeader + '" data-stream-name="' + obj.dataStreamName + '">' + obj.dataStreamHeader + '<i class="close-right"></i><i class="chev-right"></i></div>');
});
if(localStorageIsEnabled()) {
var localStorageName = window.username + '-history-container';
if(typeof localStorage[localStorageName] != "undefined") {
$('#history-container').css('display','block');
$('#history-container').html('');
var historyContainer = $.parseJSON(localStorage[localStorageName]);
$.each(historyContainer, function(key,obj) {
$('#history-container').append('<div class="stream-selection" data-stream-header="' + obj.dataStreamHeader + '" data-stream-name="' + obj.dataStreamName + '">' + obj.dataStreamHeader + '<i class="close-right"></i><i class="chev-right"></i></div>');
});
}
updateHistoryLocalStorage();
}
updateHistoryLocalStorage();
}

View File

@ -63,8 +63,23 @@ $('<img/>').attr('src', window.fullUrlToThisQvitterApp + 'img/ekan4.jpg').load(f
$('#user-container').css('display','block');
$('#feed').css('display','block');
// check for localstorage, if none, we remove possibility to remember login
var userInLocalStorage = false;
if(localStorageIsEnabled()) {
if(typeof localStorage.autologinUsername != 'undefined') {
userInLocalStorage = true;
}
}
else {
$('input#rememberme').css('display','none');
$('span#rememberme_label').css('display','none');
$('#remember-forgot').css('font-size','0');
$('.language-dropdown').css('display','none');
}
// autologin if saved
if(typeof localStorage.autologinUsername != 'undefined') {
if(userInLocalStorage) {
$('input#username').val(localStorage.autologinUsername);
$('input#password').val(localStorage.autologinPassword);
$('#submit-login').trigger('click');
@ -116,8 +131,10 @@ $('#submit-login').click(function () {
// if remeber me is checked, save credentials in local storage
if($('#rememberme').is(':checked')) {
localStorage.autologinPassword = $('input#password').val();
localStorage.autologinUsername = $('input#username').val();
if(localStorageIsEnabled()) {
localStorage.autologinPassword = $('input#password').val();
localStorage.autologinUsername = $('input#username').val();
}
}
// load history
@ -186,8 +203,10 @@ $('input#username,input#password,input#rememberme').keyup(function(e){
· · · · · · · · · · · · · */
$('#logout').click(function(){
delete localStorage.autologinUsername;
delete localStorage.autologinPassword;
if(localStorageIsEnabled()) {
delete localStorage.autologinUsername;
delete localStorage.autologinPassword;
}
location.reload();
});
@ -206,7 +225,9 @@ $(document).bind('click', function (e) {
}
});
$('.language-link').click(function(){
localStorage.selectedLanguage = $(this).attr('data-lang-code'); // save langage selection
if(localStorageIsEnabled()) {
localStorage.selectedLanguage = $(this).attr('data-lang-code'); // save langage selection
}
location.reload(); // reload
});