SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Enter variable into URL

    My client is using an ad tracking script in his shopping cart, so I need to put a variable into a string and send it off to the tracker. The variable is provided by the shopping cart via javascript, and I'm not sure how to put that into the string.

    This is what I came up with, but I have no way of knowing if it works because I can't actually see the output.

    Code:
    <html>
    <head>
    <script>
        function orderNum() {
            document.write(ss_ordernum); // cart provided variable
        }
    </script>
    
    <script src="https://tags.mediaforge.com/js/xxx/?orderNumber=javascript:orderNum();">
    
    </head>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tgavin View Post
    My client is using an ad tracking script in his shopping cart, so I need to put a variable into a string and send it off to the tracker. The variable is provided by the shopping cart via javascript, and I'm not sure how to put that into the string.

    This is what I came up with, but I have no way of knowing if it works because I can't actually see the output.
    No that wouldn't work. Which ad tracking script is being used there?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the delay, I didn't receive notification you'd posted!

    it's MediaForge.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    One way to do it is to use JavaScript to create a new script tag, so that you can use the order number in the src attribute of the script tag, but that then means that situations without scripting support won't see the ad.

    The best way that I know of to handle this is to pass the order number to a PHP script so that the PHP script can generate the page (and/or just the script tag) with that order number in the ad-tracking part. That way when JavaScript is supported, you can make an ajax request to the PHP script for that same script reference and add it to the existing page as well.

    So the two different streams are:

    Shopping cart -> PHP script (which includes generateAdScriptLink.php) -> HTML page with order number
    or
    Shopping cart -> Ajax request for generateAdScriptLink.php -> insert/replace the requested content in to the page

    Which would be something like the following example:

    HTML Code:
    <form id="shoppingCart" method="post" action="handleShoppingCartOrder.php">
        <input name="ordernumber">
        ...
    </form>
    handleShoppingCartOrder.php
    PHP Code:
    <?php
    ob_start
    ();
    include 
    'generateAdScript.php'// echos the output, so it can be used by ajax as well
    $adScriptLink ob_get_clean();

    $adScript '';
    if (!empty(
    $adScripLink)) {
        
    $adScript '<script src="' $adScriptLink '"></script>';
    }

    echo <<< EOT
    <html>
        <head>
            ...
            
    $adScript
        </head>
        ...
    </html>

    EOT;
    ?>
    generateAdScript.php
    PHP Code:
    <?php
    $orderNumber 
    filter_input(INPUT_POST'ordernumber'FILTER_SANITIZE_NUMBER_INT);
    if (
    $orderNumber 0) {
        echo 
    'https://tags.mediaforge.com/js/xxx/?orderNumber=' $orderNumber;
    }
    ?>
    Code javascript:
    var shoppingCart = document.getElementById('shoppingCart');
    shoppingCart.onsubmit = function () {
        // use ajax techniques to submit this.elements.ordernumber to generateAdScript.php and use the parseResponse function to process the result
        return false; // prevent form from automatically submitting
    };
     
    function parseResponse(request) {
        // check if a valid Ajax response
        // ...
     
        // and then
        addScriptLinkToHead(request);
    }
     
    function addScriptLinkToHead(link) {
        var head = document.head,
            adScriptLink = request,
            scriptTag = document.createElement('script')
            oldScriptTag = document.getElementById('generatedScriptTag');
        scriptTag.src = adScriptLink;
        scriptTag.id = 'generatedScriptTag';
     
        // remove old script tag if necessary
        if (oldScriptTag) {
            oldScriptTag.parentNode.removeChild(oldScriptTag);
        }
     
        // add script tag
        head.appendChild(scriptTag);
    }

    The above code has been written from off the top of my head, and is completely untested so don't expect any of it to work.
    Given that though, the way that the code is structured is the sort of thing that I'm talking about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! That's a good idea. The bad thing about this cart is that only HTML and javaScript can be used in this section, so AJAX would be a nice solution. I don't see in your example the javascript variable (ss_ordernum) which is how the cart returns the order number. How do I get that?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tgavin View Post
    Thank you! That's a good idea. The bad thing about this cart is that only HTML and javaScript can be used in this section, so AJAX would be a nice solution. I don't see in your example the javascript variable (ss_ordernum) which is how the cart returns the order number. How do I get that?
    I couldn't answer that at this stage as we have no information yet about the cart that you are speaking about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Figured that part out.

    Thanks so much, Paul. You've put me on the right track. I really appreciate it!


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
  •