Adding JavaScript: Difference between revisions
From Joomla! Documentation
No edit summary |
Added the JHTML method of loading a js file |
||
| Line 53: | Line 53: | ||
Reload your template and view the page, and ensure that the sample.js is included in the <head> | Reload your template and view the page, and ensure that the sample.js is included in the <head> | ||
== Adding Javascript Files Using JHTML == | |||
You may also use the [http://api.joomla.org/Joomla-Framework/HTML/JHTML.html JHTML] [http://api.joomla.org/Joomla-Framework/HTML/JHTML.html#script script] method to add a javascript file to the head of your document. | |||
<?php | |||
$filename = 'path/to/file/filename.js'; | |||
JHTML::script($filename); | |||
?> | |||
Revision as of 05:55, 15 January 2009
| |
This article is a small, well-defined item that could be completed by someone with a reasonable knowledge of the subject matter and a modest time commitment. If you would like to try writing this article you're welcome to do so.
The subject may be self-evident, but if not then further details should be available on the discussion page. Please add {{inuse}} at the top of this page while editing. For other small, well-defined tasks, please look in the Cookie jar. ---Thank you. This article was last edited by Rfriedel (talk| contribs) 17 years ago. (Purge) |
Adding JavaScript
This chunk should describe in detail how to add JavaScript to the head of a template using the Joomla! 1.5 API calls. It should be aimed at people who have only minimal knowledge of PHP, HTML and JavaScript.
Add the following code to have the javascript library /media/system/js/sample.js included in your template.
<?php $document = &JFactory::getDocument(); $document->addScript( '/media/system/js/sample.js' ); ?>
Explanation
Ultimately you are trying to have the resulting HTML page have in the <head> ... </head> area have a javascript include:
For Example:
<script type="text/javascript" src="/media/system/js/sample.js"></script>
Ensure that the javascript you want to include is in the directory, from the above example:
/media/system/js/sample.js
When you are able to load both your page and see the <script> tag in the <head> area, and be able to load the javascript from the address
Again, using the example:
http://www.example.com/media/system/js/sample.js
Then the script is integrated into your page, and you can proceed to using javascript in your HTML.
Do not directly add the <script> to your template's index.php.
The code will insert the the <script> line where your index.php has the following line:
<jdoc:include type="head" />
Add this PHP code to your page, in the head, or next to the javascript code you will use, depending on your preference.
<?php $document = &JFactory::getDocument(); $document->addScript( '/media/system/js/sample.js' ); ?>
Reload your template and view the page, and ensure that the sample.js is included in the <head>
Adding Javascript Files Using JHTML
You may also use the JHTML script method to add a javascript file to the head of your document.
<?php $filename = 'path/to/file/filename.js'; JHTML::script($filename); ?>
