用于计算产品总数的脚本

Essentially, I am looking for a script to count the total number of products that I have in my store and then place this total inside a div.

Seems like what you need is NOT javascript at all. If you are talking about OpenCart (as your title suggests), you need access to the total number of the products in your database (not in the page itself or DOM elements).

The 'professional approach' will be to extend your model and controller files with the correct methods, but in case you are trying to avoid it (can't really recommend it) this is what you need to do.

<div>
<?php
  $query = $this->db->query("SELECT status FROM " . DB_PREFIX . "product WHERE status='1' ");
  $products_count = $query->rows ? count($query->rows) : 0;
  echo "Total products in store: " . $products_count;
?>
</div>

Again, since I'm a big fan of MVC structure, I'd recommend extending the controller and model to handle all the data, especially if you're going to use this in multiple view files. This way, you'll send the data to the view and all you'll have to do in your code will be:

<div>
   echo "Total products in store: " . $products_count;
</div>

Hope this helps!

In controller

$this->load->model('catalog/product'); 
//loads product model

$this->data['total_product'] = $this->model_catalog_product->getTotalProducts(); 
// gives the number of product

In template file

echo $total_product

This can be the most simple way to display total product, if you want to do this by script function Then define a function in Controller and call it using json from tpl.