How to Retain State on Page Refresh?

There are only 3 Questions asked in second round of interview

1- How to retain the value of html by using javascipt Only – No Server Side code, No Session and No ViewState or Hidden field, this is a only test.html page
2- Why we need to dispose the object of SPSite and SPWeb
3- Give any sceniors where we dont need to dispose the Object of SPSite

Question 1– There are some check box on test.html page and we need to retain the values of checked checkbox after postbacking the page .

In modern browsers, easier and preferred method is to use window.localStorage or sessionStorage for storing site wide variables. To accommodate older browsers as well, you can maybe consider using cookies as a secondary storage location

sessionStorage maintains a separate storage area for each given origin that’s available for the duration of the page session (as long as the browser is open, including page reloads and restores)

localStorage does the same thing, but persists even when the browser is closed and reopened.

Browser compatibility for window.localStorage or sessionStorage
localStorage – Chrome 4, Firefox 3.5, Internet Explorer 8, Opera 10.50, Safari 4
sessionStorage – Chrome 5, Firefox 2, Internet Explorer 8, Opera 10.50, Safari 4

Set the value
var someVarName = “value”;
localStorage.setItem(“someVarName”, someVarName);

Get the value
var someVarName = localStorage.getItem(“someVarName”);
Set the value
sessionStorage.setItem(“variableName”,”test”);
Get the value
sessionStorage.getItem(“variableName”);
in case of cookies

document.cookie = _key+’=’+_value+’; expires=<date-here>; path=/; ;domain=<.yourdomain>’;
if(document.cookie.indexOf(_key+’=’) != -1) {
return document.cookie.split(_key+’=’)[1].split(‘;’)[0];
}
so for answers for question by using store it’s state within local storage and restore it after each page load.
$(‘#myCheckbox’).click(function () {
localStorage[‘my.checkbox’] = this.checked;
});

Restore it:

$(‘#myCheckbox’).prop(‘checked’, localStorage[‘my.checkbox’] == ‘true’);

Updated: March 1, 2017 — 12:48 PM

Leave a Reply

Your email address will not be published. Required fields are marked *

3 + 7 =


Tech Contents © 2016
Inline
Inline