Hi,
PHP would be more suitable, Javascript will never be 100% reliable/foolproof, but if that's no problem it's not impossible to do with JS.
If you have a cart module on all your pages for example, you can easily write a few lines of JS code to check if there are products in this cart module or not. There's also a trigger event which you can readily use for when the user adds or removes a product from the cart:
window.Oby.registerAjax(["cart.updated","wishlist.updated"],function(params){
/*
params.type : 'cart' or 'wishlist'
params.id : the cart id. If NaN, it has failed.
params.el : the dom element where the customer clicked to add the product into the cart
params.product_id : the product id
params.resp : object
ret : the cart id, if 0, it has failed
product_name : the name of the product
quantity : the quantity of the product
image : the url of the product image (50x50)
*/
});
With this you should be able to achieve what you want. Note that there can be a delay before the Ajax call is finished and the products are updated in the cart module which you have to take into account.
Hope that helps!