1.What is Window SessionStorage?
Answer:
The sessionStorage stores data for only one session.The data is removed or deleted when the browser is closed.
Eg:-//store
sessionStorage.setItem("username","Shiva");
//retrieve
sessionStorage.getItem("username"); //Shiva
2.What is Window LocalStorage?
Answer:
The localstorage properties allow to save key/value pairs in a web browser.
The important point about localstorage is it stores the data with no expiration date.The data will not be
deleted when the browser is closed and it will be available even on next day or next week when you view it on web browser.
Eg:-//store
localStorage.setItem("username","Muruga");
//retrieve
localStorage.getItem("username"); //Muruga
3.What is Json.Parse?
Answer:
It means interpret or read the data given in the specific format (i.e JSON).So JSON.parse means it reads the string and converts that
string to javascript object.The job of the parser is to convert the string into objects.
Let us understand this by this simple example,
Example:-
var str='{"name": "Shiva"}';
var obj=JSON.parse(str);
BEFORE PARSING:-
before parsing it is a string and you cannot access the data inside it
console.log(str.name) // prints "undefined"
AFTER PARSING:-
after parsing it becomes javascript object and you can access its properties and methods.So you get the name "Shiva" during printing of obj.name
console.log(obj.name) // prints "Shiva"
4. How to get data from server side and display it on client side javascript using $.ajax get?
Scenario:
For example, i have 2 files named Example1.aspx and Example2.aspx
In example1.aspx i am saving currentdate in session.I need to get the current date in example2.aspx clientside.
In example2.aspx, i have button as getcurrentdate.If i click button in example2.aspx,it should go to example1.aspx.cs file where i have stored currentdate in session and return back to example2.aspx.
How to achieve?
Solution:
Example2.aspx:-
In ajax get request, you cannot pass text as datatype and get data from server (i.e example1.aspx) eventhough it is text data,
because default datatype is json for both get and post request.
Example1.aspx.cs:-
[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string GetCurrentDate()
{
string ss= HttpContext.Current.Session["hdncurrentdate"].ToString();
return ss;
}
public void SavingDateinSession()
{
Session["hdncurrentdate"]=datetime.now();
}
<input type="button" onclick="Fetchcurrentdate()" text=Fetchcurrentdate>
function Fetchcurrentdate()
{
$.ajax({
url: '/Example1.aspx/GetCurrentDate',
type: 'GET',
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{}',
success: function (data) {
currentdate = data.d;
},
error: function (xhr) {
},
complete: function (data) {
}
});
}
5.How to find the length of JSON Parsed object?
Answer:
Let us say you are parsing a json response like the following
var obj = JSON.Parse(response);
let us say you have output like the below,
so the code you need to provide is ,
response:{ "details":"server error",
"code":"",
"message":"current endpoint address is not correct"}
var lenofobj=Object.Keys(obj).length;
console.log("Length of object is",lenofobj)
The output is,
Length of object is 3
6.How will you find whether an object contains data or not?
Answer:
you can use typeof operator to find whether the object contains data or not.Suppose if the object contains no data
it will return as 'undefined' otherwise it will return with data.
so the code you need to provide is ,
if(typeof(obj)!='undefined')
{
console.log("use Typeof", typeof(obj))
}
else
{
console.log("Object is undefined")
}
var lenofobj=Object.Keys(obj).length;
console.log("Length of object is",lenofobj)
The output is,
Length of object is 3