SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code 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:
    Code:
    <form style='display:none;' name='ClientID'>
    <input type="hidden" name="ClientID" value="<%= Session("ClientID") %>">
    </form>
    Code JavaScript:
    orderId : '<DCCClientID2>',

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    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 :-)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working. I had some errors with the code. Extra brackets and double quotes. Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •