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 browserLang = navigator.language || navigator.userLanguage;
var selectedLanguage = 'en'; var selectedLanguage = 'en';
if(typeof localStorage.selectedLanguage != 'undefined') { if(localStorageIsEnabled()) {
selectedLanguage = localStorage.selectedLanguage; if(typeof localStorage.selectedLanguage != 'undefined') {
} selectedLanguage = localStorage.selectedLanguage;
else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') { }
selectedLanguage = browserLang.substring(0,2); else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') {
selectedLanguage = browserLang.substring(0,2);
}
} }
window.sL = window.l[selectedLanguage]; window.sL = window.l[selectedLanguage];

View File

@ -33,7 +33,29 @@
· · · ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
/* ·
·
· 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() { function updateHistoryLocalStorage() {
var i=0; if(localStorageIsEnabled()) {
var localStorageName = window.username + '-history-container'; var i=0;
var historyContainer = new Object(); var localStorageName = window.username + '-history-container';
$.each($('#history-container .stream-selection'), function(key,obj) { var historyContainer = new Object();
historyContainer[i] = new Object(); $.each($('#history-container .stream-selection'), function(key,obj) {
historyContainer[i].dataStreamName = $(obj).attr('data-stream-name'); historyContainer[i] = new Object();
historyContainer[i].dataStreamHeader = $(obj).attr('data-stream-header'); historyContainer[i].dataStreamName = $(obj).attr('data-stream-name');
i++; historyContainer[i].dataStreamHeader = $(obj).attr('data-stream-header');
}); i++;
localStorage[localStorageName] = JSON.stringify(historyContainer); });
if($('#history-container .stream-selection').length==0) { localStorage[localStorageName] = JSON.stringify(historyContainer);
$('#history-container').css('display','none'); 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() { function loadHistoryFromLocalStorage() {
var localStorageName = window.username + '-history-container'; if(localStorageIsEnabled()) {
if(typeof localStorage[localStorageName] != "undefined") { var localStorageName = window.username + '-history-container';
$('#history-container').css('display','block'); if(typeof localStorage[localStorageName] != "undefined") {
$('#history-container').html(''); $('#history-container').css('display','block');
var historyContainer = $.parseJSON(localStorage[localStorageName]); $('#history-container').html('');
$.each(historyContainer, function(key,obj) { var historyContainer = $.parseJSON(localStorage[localStorageName]);
$('#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>'); $.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'); $('#user-container').css('display','block');
$('#feed').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 // autologin if saved
if(typeof localStorage.autologinUsername != 'undefined') { if(userInLocalStorage) {
$('input#username').val(localStorage.autologinUsername); $('input#username').val(localStorage.autologinUsername);
$('input#password').val(localStorage.autologinPassword); $('input#password').val(localStorage.autologinPassword);
$('#submit-login').trigger('click'); $('#submit-login').trigger('click');
@ -116,8 +131,10 @@ $('#submit-login').click(function () {
// if remeber me is checked, save credentials in local storage // if remeber me is checked, save credentials in local storage
if($('#rememberme').is(':checked')) { if($('#rememberme').is(':checked')) {
localStorage.autologinPassword = $('input#password').val(); if(localStorageIsEnabled()) {
localStorage.autologinUsername = $('input#username').val(); localStorage.autologinPassword = $('input#password').val();
localStorage.autologinUsername = $('input#username').val();
}
} }
// load history // load history
@ -186,8 +203,10 @@ $('input#username,input#password,input#rememberme').keyup(function(e){
· · · · · · · · · · · · · */ · · · · · · · · · · · · · */
$('#logout').click(function(){ $('#logout').click(function(){
delete localStorage.autologinUsername; if(localStorageIsEnabled()) {
delete localStorage.autologinPassword; delete localStorage.autologinUsername;
delete localStorage.autologinPassword;
}
location.reload(); location.reload();
}); });
@ -206,7 +225,9 @@ $(document).bind('click', function (e) {
} }
}); });
$('.language-link').click(function(){ $('.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 location.reload(); // reload
}); });