Click here to Skip to main content
15,895,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I am developing one application in which i want to clear session and redirect user to another page by using java script function.
Can anybody help me for same.

Thanks in advance. :-)
Posted

Hi,

We can not clear server side session from javascript code.

but we can do with xmlhttprequest or Jquery.

just by using code like following

JavaScript
  function logout()
{
  $.post("logout.aspx?action=logout",{},function(data){
    location.href="logout.aspx";
});
}


In code behind file contain following code for peform logout operation

C#
//under pageload of logout.aspx
if(request.Querystring["action"]!=null)
{
  response.clear();
  Session.abondon();
 response.write("Success");
response.End();
}


All the Best
 
Share this answer
 
Comments
saj_21 14-Oct-11 5:02am    
yes that's right but i want something which clears the session at client side and redirects him to another page.
here is my code.

Javascript code :

function setSessionOff()
{
var sessionInfo = NewSession();
sessionInfo.Item("UserId") = null;
sessionInfo.Item("hdata") = null;
SetSession(sessionInfo);
window.location = "home.html";
}

aspx code :

Home
but its not working :-(
Muralikrishna8811 14-Oct-11 5:06am    
is it works for you

how you find that NewSession() javascript object

and what exactly in that object

can you post that Session object url

Jonathan Camargo O 20-Oct-14 20:35pm    
hi every one,

this answer run only mozilla firefox!!
sessionInfo.Item("UserId") = null;
sessionInfo.Item("hdata") = null;
SetSession(sessionInfo);

...but in chrome don't run.... please help me for put have this javascript in others browser....
saj_21 21-Oct-11 4:07am    
thanks friend.
the above solution given by you is working. :-)
Jonathan Camargo O 17-Oct-14 20:03pm    
hi every one,

this answer run only mozilla firefox!!
sessionInfo.Item("UserId") = null;
sessionInfo.Item("hdata") = null;
SetSession(sessionInfo);

...but in chrome don't run.... please help me for put have this javascript in others browser....
Refer this, might help you
https://www.owasp.org/index.php/HTTPOnly[^]
 
Share this answer
 
1.Add CSS in your page
<style>.hidden { display: none; }</style>

2.Add this script
function ClearSession() { document.getElementById("btnHidden").click(); }

3.Add a hidden control like this one
<asp:Button ID="btnHidden" runat="server" CssClass="hidden"
OnClick="Button1_Click" ClientIDMode="Static" />

4.Add this one in your code behind
protected void Button1_Click(object sender, EventArgs e) { Session["smn"] = null; }
Now on your control you just need to call the javascript above, pretty much the same as yours:

<ClientSideEvents CloseButtonClick="ClearSession()" />

 
Share this answer
 
hi every one,

this answer run only mozilla firefox!!
sessionInfo.Item("UserId") = null;
sessionInfo.Item("hdata") = null;
SetSession(sessionInfo);

...but in chrome don't run.... please help me for put have this javascript in others browser....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900