Passing Session Data into Java Script

I have a session variable (ClientID) that i want to pass into Javascript which will in turn send out to my 3rd party tracking company. I’ve tried several methods of passing the data. I tried using <%= Session(“ClientID”) %>. But I read you cannot do it this way as Javascript is client side and session in server side. So I read you could create a hidden form field and then call the element id in the javascript. This did not work either, but I think it might have been bad syntax.

I know the ClientID is being grabbed from the database because I can print it to the page. Just not to the Javascript.

Is it a matter of syntax? Or am I just doing it wrong. I could not find much in Google. Mostly it was on how to do it the other way, passing javascript variables to ASP sessions. Any help would be appreciated. Thanks.

Here is the javascript:


<script type="text/javascript">
var _mTrack = _mTrack || [];

_mTrack.push(['addTrans', {
orderId : "<%= Session("clientID") %>",
total : ,
tax : ,
shipping : ,
}]);

_mTrack.push(['processOrders']);
    (function() {
        var mClientId = '[123456789]';
        var mProto = ('https:' == document.location.protocol ? 'https://' : 'http://');
        var mHost = 'tracker.xxx.com';
        var mt = document.createElement('script'); mt.type = 'text/javascript'; mt.async = true; mt.src = mProto + mHost + '/tracker/async/' + mClientId + '.js';
        var fscr = document.getElementsByTagName('script')[0]; fscr.parentNode.insertBefore(mt, fscr);
    })();
</script>
<noscript>
    <img src="https://tracker.xxx.com/tp?act=2&cid=[123456789]&script=no" >
</noscript>

I also tried using this hidden form and the javascript:


<form style='display:none;' name='ClientID'>
<input type="hidden" name="ClientID" value="<%= Session("ClientID") %>">
</form>


orderId : '<DCCClientID2>',

You should be able to render it out to the page (and thus, to JavaScript). You should be able to pass any server side vars to JavaScript at server render time.

You may simply need to check your syntax around:


orderId : "<%= Session("clientID") %>",

(Though I suspect the outside quotes will be ignored by your server side code.)

If you do a view source you should be able to see if the client ID has been rendered into the JS or not :slight_smile:

Thanks for your help. I looked at the source code and verified that the ClientID is being populated in the javascript. So I guess the javascript is just not firing off to the 3rd party. But I think this is on their end now and not mine?

It might be a good idea to verify there are no JavaScript errors (check the console in Firebug or Chrome Dev Tools). You should probably also be seeing a request made to the tracker in the Network tab in one of Firebug / Chrome Dev Tools.

Got it working. I had some errors with the code. Extra brackets and double quotes. Thanks