How to Add javascript in Joomla

Share:
There are three ways to add javascript

Inline JavaScript

1:  <?php  
2:  $document = JFactory::getDocument();  
3:  $document->addScriptDeclaration('  
4:    window.event("domready", function() {  
5:      alert("An inline JavaScript Declaration");  
6:    });  
7:  ');  
8:  ?>  

External JavaScript There are two ways to include a JavaScript file

1:  <?php  
2:  $document = JFactory::getDocument();  
3:  $document->addScript('/media/system/js/sample.js');  
4:  ?>  

The second

1:  <?php  
2:  // Add the path parameter if the path is different than 'media/system/js/'  
3:  JHTML::script('sample.js', 'templates/custom/js/');  
4:  ?>  

API has changed in 3.x, so the second parameter cannot be a string. If you really need to use this method, you must include the absolute link to your javacript file:

1:  <?php  
2:  JHtml::script(Juri::base() . 'templates/custom/js/technology.js');  
3:  ?>  

No comments

Ads