Joomla! provides us with some functions so, you can add the Javascript in the correct place, this functions can be called from anywhere in the code and they will not break compatibility with the XHTML standard. These functions are addScriptDeclaration and addScript they allow us to insert Javascript code and insert a Javascript (.js) file.
Adding Javascript using the addScript function:
If you will like to add a external .js file you will use the addScript function and pass as a argument the path and name of the .js file you should also specify the whole path of your Javascript file by using the JURI:base function. In this case, the hello.js file is in the root folder of the Joomla! installation.
In a template you will call it the following way using the this variable, because the template is called by the document class where the addScript function is located.
Note: we add JURI::base() before the hello.js to prefix the path of the folder where Joomla! resides and $this->template to get the name of the folder where the template resides.
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$js = "/templates/".$this->template."/hello.js";
$this->addScript(JURI::base() . $js);
?>
If you where calling this from a component or module you will need to get the document reference using this line of code. This line of code will store the document reference into the document variable.
$document =& JFactory::getDocument();
And call the function to add the javascript file.
Note: we add JURI::base() before the hello.js to prefix the path of the folder where Joomla! resides.
defined('_JEXEC') or die();
jimport( 'joomla.application.component.view');
class AlliesViewAllies extends JView
{
function display($tpl = null)
{
global $mainframe, $option;
$document =& Jfactory::getDocument();
$js = "/templates/".$this->template."/hello.js";
$document->addScript(JURI::base() . $js);
Adding Javascript using the addScriptDeclaration function:
If you will like to add a small piece of Javascript code you will use the addScriptDeclaration function. Example:
If you are calling it inside a template you will use the $this variable and inside the double quotes you will pass it Javascript code in this case an alert function that will display Hello Joomla! To the user.
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$this->addScriptDeclaration("alert('Hello Joomla!')");
?>
If you will like to add this Javascript from a module or a component you will do it by getting a reference of the document using the getDocument Function:
$document =& Jfactory::getDocument();
And now you call the function using the document variable
$document->addScriptDeclaration("alert('Hello Joomla!')");
the code will like like this
defined('_JEXEC') or die();
jimport( 'joomla.application.component.view');
class AlliesViewAllies extends JView
{
function display($tpl = null)
{
global $mainframe, $option;
$document =& JFactory::getDocument();
$document->addScriptDeclaration("alert('Hello Joomla!')");
default menu item is of type front page layout
padding-left: 10px; padding-right:10px; padding-top: 5px; padding-bottom:5px;
margin-left: 0px; margin-right:0px; margin-top: 0px; margin-bottom:0px;
by adding margin column in a hover the menu appears different
Joomla maintains a media manager to insert images ..media manager is in article content bottom
countModules('left')) : ?>-----module title will display only if we use this tag
defined( '_JEXEC' ) or die( 'Restricted access' );--- it is used to not allowing the direct access of the web pages
Comments