Created
February 25, 2022 22:40
-
-
Save vfontjr/6dcd9819a6f15770217d61ec4beec540 to your computer and use it in GitHub Desktop.
Broser Storage jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* functions to create a persistent form */ | |
function browser_supports_storage() { | |
return ( typeof(Storage) !== "undefined" ) ? true : false; | |
} | |
function local_data_exists() { | |
return ( localStorage.getItem('data') ) ? true : false; | |
} | |
function cookie_is_used_for_persistence() { | |
return ( Cookies.get('hpw_search') ) ? true : false; | |
} | |
function save_form_to_storage() { | |
calc_slider_positions(); | |
var data = JSON.stringify($('form#hpw_search').serializeJSON({checkboxUncheckedValue: "no"})); | |
if ( browser_supports_storage() ) { | |
localStorage['data'] = data; | |
} | |
/* always use cookie to access data in PHP */ | |
Cookies.set('hpw_search', data); | |
} | |
function restore_form_from_storage() { | |
var saved_data = ""; | |
if ( browser_supports_storage() && local_data_exists() ) { | |
saved_data = JSON.parse(localStorage.getItem('data')); | |
} else if ( cookie_is_used_for_persistence() ) { | |
/* restore field values */ | |
saved_data = Cookies.getJSON('hpw_search'); | |
} | |
if (saved_data) { | |
restore_form_field_values( saved_data ); | |
/* restore slider width and handle positions */ | |
restore_slider_positions(); | |
refresh_sliders(); | |
} | |
} | |
function delete_form_from_storage() { | |
if ( browser_supports_storage() && local_data_exists() ) { | |
localStorage.removeItem('data'); | |
} | |
if ( cookie_is_used_for_persistence() ){ | |
Cookies.remove('hpw_search'); | |
} | |
} | |
function restore_form_field_values( saved_data ) { | |
if( ! ( saved_data ) ) { | |
return; | |
} | |
var field_type = ''; | |
$.each(saved_data, function(key, val) { | |
field_type = (key.search('select') !== -1 ) ? 'select' : $('input[name="' + key + '"]').attr('type'); | |
switch(field_type) { | |
case 'checkbox' : | |
if (val === 'yes') { | |
$('input[name="' + key + '"]').prop('checked', true); | |
// $('input[name="' + key + '"]').trigger('click'); | |
reset_inputs($('input[name="' + key + '"]')); | |
} | |
break; | |
case 'radio': | |
$('input[name="' + key + '"][value="' + val + '"]').prop('checked',true); | |
// $('input[name="' + key + '"]').trigger('click'); | |
reset_inputs($('input[name="' + key + '"]')); | |
break; | |
case 'select': | |
$('select[name="' + key + '"]').val(val); | |
$('select[name="' + key + '"]').trigger('chosen:updated'); | |
break; | |
default : | |
$('input[name="' + key + '"]').val(val); | |
if (key === 'plan_count') { | |
$('div#search-results').html('Available Plans: ' + val); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment