[development] How can I make Drupal DOM elements accessible within a javascript function without having to do an ajax request to a page?

John Mitchell mitchelljj98 at gmail.com
Sun Feb 6 07:10:51 UTC 2011


I have a javascript timer set to fire every 2.5 seconds so that I can set
the current values for the items and total for the shopping cart summary.  I
would like to be able to get these values from client side cookies or
javascript global variables so that I don't have to make an ajax call to the
server but I have found that the DOM elements are not accessible within the
javascript function unless I run an ajax request to a page (listed below)
and after it returns then the DOM elements are accessible (i.e.
$(".num-items").html(numItems);) and now I can update them.  Having these
ajax requests run every 2.5 seconds for every client will not scale.

How can I make Drupal DOM elements accessible within a javascript function
without having to do an ajax request to a page?

Thanks,

John

    setInterval('updateShoppingCartInfo()', 2500);

    function updateShoppingCartInfo(){
        var xmlHttpReq = new XMLHttpRequest();
        var url = 'https://' + document.domain + '/cart/refreshcarttimer';
        xmlHttpReq.open('post', url, true);
        xmlHttpReq.onreadystatechange = function() {
               if (xmlHttpReq.readyState != 4)  {
                   return;
               }
            else {
                var responseText = xmlHttpReq.responseText;
                                var numItems =
responseText.substr(0,responseText.indexOf(","));
                                var ucPrice =
responseText.substr(responseText.indexOf(",")+1);
                                $(".num-items").html(numItems);
                                $(".uc-price").html(ucPrice);
            }
        }
        xmlHttpReq.send(null);
    }


-- 
John J. Mitchell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110206/a932f1f1/attachment.html 


More information about the development mailing list