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,15 +651,17 @@ 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(localStorageIsEnabled()) {
if(typeof localStorage.selectedLanguage != 'undefined') { if(typeof localStorage.selectedLanguage != 'undefined') {
selectedLanguage = localStorage.selectedLanguage; selectedLanguage = localStorage.selectedLanguage;
} }
else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') { else if(typeof window.l[browserLang.substring(0,2)] != 'undefined') {
selectedLanguage = browserLang.substring(0,2); selectedLanguage = browserLang.substring(0,2);
} }
}
window.sL = window.l[selectedLanguage]; window.sL = window.l[selectedLanguage];
// if this is a RTL-language, add rt classes and change some things // if this is a RTL-language, add rt classes and change some things

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,6 +255,7 @@ function placeCaretAtEnd(el) {
· · · · · · · · · · · · · */ · · · · · · · · · · · · · */
function updateHistoryLocalStorage() { function updateHistoryLocalStorage() {
if(localStorageIsEnabled()) {
var i=0; var i=0;
var localStorageName = window.username + '-history-container'; var localStorageName = window.username + '-history-container';
var historyContainer = new Object(); var historyContainer = new Object();
@ -252,6 +275,7 @@ function updateHistoryLocalStorage() {
$('#history-container').sortable({delay: 100}); $('#history-container').sortable({delay: 100});
$('#history-container').disableSelection(); $('#history-container').disableSelection();
} }
}
/* · /* ·
@ -261,6 +285,7 @@ function updateHistoryLocalStorage() {
· · · · · · · · · · · · · */ · · · · · · · · · · · · · */
function loadHistoryFromLocalStorage() { function loadHistoryFromLocalStorage() {
if(localStorageIsEnabled()) {
var localStorageName = window.username + '-history-container'; var localStorageName = window.username + '-history-container';
if(typeof localStorage[localStorageName] != "undefined") { if(typeof localStorage[localStorageName] != "undefined") {
$('#history-container').css('display','block'); $('#history-container').css('display','block');
@ -272,6 +297,7 @@ function loadHistoryFromLocalStorage() {
} }
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');
// autologin if saved // check for localstorage, if none, we remove possibility to remember login
var userInLocalStorage = false;
if(localStorageIsEnabled()) {
if(typeof localStorage.autologinUsername != 'undefined') { 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(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,9 +131,11 @@ $('#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')) {
if(localStorageIsEnabled()) {
localStorage.autologinPassword = $('input#password').val(); localStorage.autologinPassword = $('input#password').val();
localStorage.autologinUsername = $('input#username').val(); localStorage.autologinUsername = $('input#username').val();
} }
}
// load history // load history
loadHistoryFromLocalStorage(); loadHistoryFromLocalStorage();
@ -186,8 +203,10 @@ $('input#username,input#password,input#rememberme').keyup(function(e){
· · · · · · · · · · · · · */ · · · · · · · · · · · · · */
$('#logout').click(function(){ $('#logout').click(function(){
if(localStorageIsEnabled()) {
delete localStorage.autologinUsername; delete localStorage.autologinUsername;
delete localStorage.autologinPassword; delete localStorage.autologinPassword;
}
location.reload(); location.reload();
}); });
@ -206,7 +225,9 @@ $(document).bind('click', function (e) {
} }
}); });
$('.language-link').click(function(){ $('.language-link').click(function(){
if(localStorageIsEnabled()) {
localStorage.selectedLanguage = $(this).attr('data-lang-code'); // save langage selection localStorage.selectedLanguage = $(this).attr('data-lang-code'); // save langage selection
}
location.reload(); // reload location.reload(); // reload
}); });