Questions? Feedback? powered by Olark live chat software

Check out 80+ FREE Joomla! templates See 80+ 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!

80+ Premium Joomla templates just $74.25! 80+ Premium WordPress themes just $74.25!

Leave a Reply

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

Flash Sale! Get 50% OFF for your purchase! Coupon code: XMASNEWYEAR50 More Details
Flash Sale! Get 25% OFF for your purchase! Coupon code: FLASHSALE25 Learn more