最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to setget session values from javascript - Stack Overflow

programmeradmin1浏览0评论

I want to access to session in javascript code so as to set and get some values : i try with this code :

function getsessionvalue() {

    var value= '<%= session["role"].ToString() %>';
    alert(value);
    //var role1= '<%= session["role"] %>'; **the same mistake**
    //alert(role1);     
}

but i have these javascript mistakes for both :

The type of the expression must be an array type but it resolved to 
Type mismatch: cannot convert from String to int

I want to access to session in javascript code so as to set and get some values : i try with this code :

function getsessionvalue() {

    var value= '<%= session["role"].ToString() %>';
    alert(value);
    //var role1= '<%= session["role"] %>'; **the same mistake**
    //alert(role1);     
}

but i have these javascript mistakes for both :

The type of the expression must be an array type but it resolved to 
Type mismatch: cannot convert from String to int
Share Improve this question asked May 23, 2014 at 17:10 lilyanalilyana 1511 gold badge4 silver badges17 bronze badges 3
  • 2 The session is and remains server side. If you need something available client side try cookies or simply inserting it on page creation. What server software are you using? – EWit Commented May 23, 2014 at 17:12
  • stackoverflow.com/questions/6918314/… – carter Commented May 23, 2014 at 17:22
  • that's not a javascript problem; js has no trouble converting types... – dandavis Commented May 23, 2014 at 17:36
Add a comment  | 

3 Answers 3

Reset to default 8

you can't access server session in client side. but if you want to do some changes in client side according to server session value. i will give you small idea.it may work for you.

(sorry, I know only java not php,etc.,)

just inside JSP script-let check for the session, create some hidden html element with session value. like this

<% String role=request.getSession().getAttribute("role").toString();%>
<input type="hidden" id="role" value=<%= role ;%> />

And then ,in javascript just get the role from html input element by ID like this.

var role=document.getElementById("role");

and do you stuff here.

And if you want to set role in session in javascript, it may help you

    <script>
function nameYourFunction()
{
    var role="";

    if(your condition)
    <% request.getSession().setAttribute("your variable","your values"); %>
}

</script> 

hope this works. And call your function, whenever you need.

Thru client side javascript also we can access session variables. Following is the simple code which assigns session variable to javascript variable.

<script type="text/javascript">
function getSessionName(){
var name='<%=session.getAttribute("uname")%>';
alert(name);
}</script>

This code worked fine

you can get session value in hidden field in jsp.

<s:hidden id="login_orgId" value="%{#session.org_id}"/>

发布评论

评论列表(0)

  1. 暂无评论