Problem to variable

Hi,
I am to refer to
        alert(Session["userid"] + "a");

Open in new window


on javascript but then have got nothing shown. why?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuaMin ChenProblem resolverAuthor Commented:
Here are the codes
<script>
    function buttonOption(type, needConfirm) {
        alert(Session["userid"] + "a");

Open in new window

Julian HansenCommented:
Session is a server side variable - it is not visible in the browser.
Sessions are used on the server to maintain state between page requests. JavaScript runs in the browser after server side code has finished processing. They cannot share function calls or variables.

if you want to aler the session var you need to do this

alert('<%= Session["userid"]%>' + 'a');

Open in new window

or even
alert('<%= Session["userid"]%> a');

Open in new window


What this does is output the value of Session['userid'] into the page that is being rendered - so in effect it is writing the javascript that will be run when the page loads in the browser.
HuaMin ChenProblem resolverAuthor Commented:
Sorry, it is not showing the Session variable as expected.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Julian HansenCommented:
Please post a link or browser source
Kelvin McDanielSr. DeveloperCommented:
If this code is in a .cshtml, .aspx, .ascx or .ashx file then what @Julian has suggested should work (though the Razor equivalent is different).

If the code is in a .js file then the JavaScript engine won't have any clue what Session is; you need to get the variable value to a place where JavaScript can actually reference it. The easiest way to handle this (especially without knowing anything about your application) is to put the Session variable value in a hidden field. So whatever file that uses the desired JavaScript code will have something like the following to expose the Session variable value:

<input type="hidden" id="theIdYouWantToUse" value="<%= Session["userid"]%>" />

Open in new window


Then you can use jQuery to get the value and put it into your alert:

<script>
    function buttonOption(type, needConfirm) {
        var theUserId = $('theIdYouWantToUse').val();
        alert(theUserId + 'a');
}
</script>

Open in new window


Happy Coding!
Julian HansenCommented:
<input type="hidden" id="theIdYouWantToUse" value="<%= Session["userid"]%>" />
Or
<script>var __SessionID = <%= Session["userid"]%>;</script>

Open in new window

And then in your .js file - which must be included after the above
.js File
...
alert(_SessionId + 'a');
...

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
As really, the one I've marked in above, is not the real answer to the original question.
Julian HansenCommented:
is not the real answer to the original question
I disagree. The original question was

alert(Session["userid"] + "a");
on javascript but then have got nothing shown. why?

The question was answered - the reason is that Session is not available in the browser as it is a server side variable and javascript is client side.

The question was answered  here and several suggestions were posted as to how you can inject the Session variable into your page so it is available in javascript (here and here Both of which are valid suggestions in the context of the problem. Given we don't have complete source code to look at and very little in the way of feedback other than "It is not working" there is much not more that could be done.

In summary, the original question was answered.
HuaMin ChenProblem resolverAuthor Commented:
I mean the mark I've marked helped a little bit, but not the exact answer.
HuaMin ChenProblem resolverAuthor Commented:
Hi,
can you please advise why I now cannot refer to my other open threads? Thanks
Julian HansenCommented:
I mean the mark I've marked helped a little bit, but not the exact answer.
Again I disagree.

The original question was why the Session variable was not showing up in the JavaScript alert.

The answer is: Session variables are not available client side.

The fact the question changed into another question i.e. how to make the session variable accessible in the JavaScript function - is irrelevant - but I contend that that question too was answered.

If you have a solution to the problem can you post it here so we can see how it differs from what was recommended.

Either way - in my view the original question was answered.
HuaMin ChenProblem resolverAuthor Commented:
OK, I accept the current advice.

And moderator, please allow me to re-mark the answer of this thread. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.