Home > Back-end >  javascript how to get the web page get Session variable
javascript how to get the web page get Session variable

Time:03-09

I'm trying to use a Session variable in javascript.

I have tried a number of post suggestions without success unfortinately.

I see this example all over the place.

    var userName = '<%= Session["UserName"] %>'

However that is not working, at least for me. What I am trying to do is as below.

    var qrdataString = "<qr-code data="   "XL"   ' <%= Session["style"] %>'    "></qr-code>";
    document.getElementById("styleqr").innerHTML = qrdataString; 

Using Single quotes where they appear above just returns the string as

    <%=Session["xUID"]%>   in the alert that I use to show me the returned value

Using Double quotes give me either - unexpected token: string literal - or unexpected token identifier.

Thanks

CodePudding user response:

After messing around for a while, and also stumbling on the following article, I have managed to get this going.

    https://newbedev.com/get-current-session-value-in-javascript 

FULL RESULT

HTML

    Dim xstyle: xstyle = session("style")

    <div >
      <input type="hidden" id="hdnSession" data-value= <%= xstyle %> />
    </div>

JAVASCRIPT

    var hdnString= $("#hdnSession").data('value');
    var qrdataString = "<qr-code data= "   hdnString    " ></qr-code>";
    document.getElementById("qrcode").innerHTML = qrdataString;

HTML

    <div  id="qrcode">
        javasctipt inserts the html here  <!--qr-code data="test"></qr-code -->
    </div>
  • Related