How to use Javascript handler in VirtueMart?
5 (100%) 1 vote


Check out 65+ FREE Joomla! templates See 60+ FREE WordPress themes

VirtueMart 3 supports reloading of the productdetails by an ajax call. It means we have to trigger existing JS and add new JS, and it must be triggered like if the page just got ready. The joomla method to add JavaScript is in this case not sufficient. The scripts are added to the header of the ajax call and just trashed. We must separate javascript in to two group: the javascript the page needs to load and the javascript that is doing stuff after the page loads.

 The js is added to a queque. But a name must be given, so it can be manipulated by a templater as long it is not rendered. Calling the function again with the same name, just replaces the existing script. This prevents automatically loading the js twice as long the same name is used. It gives also templaters an easy method to override the core JS by their own versions. The JS Writer is called after the function display (only in vmViews). The JS Writer adds the files then in joomla style to the header and the inline js to the end of the component container. The entry is set to written. So you can also call the Writer as often as you want. The code is additionally wrapped by //<![CDATA[ in case it is missing.

How to use it?

Just use the function vmJsApi::addJScript.

public static function addJScript($name, $script = false, $defer = true, $async = false)
The first parameter is the name to label the script. The second parameter is just your java script string. The last parameters have as default already the correct values for inline js. For libraries like jQuery, you need usually defer = false.

In case you are just loading a file, use a trailing slash /, use for absolute URLs or domains just // instead of https. It is also a valid shortcut to use as first parameter directly the relative URL to your file. Local files search for the js in /templates/yourtemplate/js, then in general Virtuemart assets path (usually components/com_virtuemart/assets/js).


//Simple call

//Adds a string as script to the end of your document 
vmJsApi::addJScript ( 'vm.cookie', 'var virtuemartcookie="'.$cookieName.'";');

//Adds a file to the header, relative to your joomla URL vmJsApi::addJScript('/administrator/components/com_virtuemart/assets/js/jquery.coookie.js');

//Adds a file to the header, relative to your js directory 

//Loads js from google libraries vmJsApi::addJScript('jquery.min','//',false);

If you are writing a module, write the JS after rendering our plugin with writeJs, example:

echo vmJsApi::writeJS();

If you want to see the already loaded JS, use

$js = vmJsApi::getJScripts();

If you have any question about this post, don’t hesitate to leave a comment below. And don’t forget to share with your friends if you find it useful! Thanks!

Join Joomla & WordPress template club to access all themes and SAVE 95% cost than single PRO template purchase!

Join Joomla! Club (50% OFF on today) Join Joomla! Club (50% OFF on today)

Leave a Reply

Your email address will not be published. Required fields are marked *

[Flash Sale!] 20% OFF for PRO order, 50% OFF for Membership! Limited time, hurry up! Save money for your order on today!
[xMas is coming!] Enjoy your purchase extra discount up to 50% OFF for all products! Coupon code: XMAS50