Shortcode invocation count as variable in template

Hi, I am creating shortcodes to add different functions. However, some of them depends on external JS, CSS and other asset files.

Is it possible to have the shortcodes invocation counts as variables in template, so that page generation can load those files only when the shortcodes is called in a page?

Why not load the asset files in a shortcode (or plain html) in the content?

The generated pages would perform better and be more organized.

Say, I have few tab containers in page. Currently I can inject JS and CSS files (assets) only in first shortcode (thanks for the invocation count), but assets are not injected in header, causing

  1. browser cannot load them in advance, slowing down the page
  2. assets injection are not organized inside header

If, in the template, I can know if the shortcode is invoked, I can inject assets in header to avoid the above these two problems.


UPDATE: One more problem, the generated pages may come with JS bugs because same assets are loaded multiple times.

If there are more than one types of shortcode, maybe tab containers and charts, that depend on same assets, those assets would be loaded twice and may causing errors. For example, loading jQuery with <script> tag more than once causes error.