Javascript include tag
From PHP on Trax
Contents |
javascript_include_tag( string $source, [string $source, ...], [array $options = array()] )
Description
Returns a script include tag per source given as argument.
$source a string which is the script's "src". There can be as many sources as desired each being a function parameter. No matter how many parameters of sources if the last parameter is an array it will be read in as options for the script tag.
The "src" can be supplied as a...
- full path, like "/my_jsscripts/myscript.js"
- file name, like "random.js", that gets expanded to "/stylesheets/random.js"
- file name without extension, like "myapp", that gets expanded to "/stylesheets/myapp.js"
$options is an array of key => value pairs of options for the script tag.
Examples
Code:
<?= javascript_include_tag("xmlhr") ?>
Output:
<script type="text/javascript" src="/javascripts/xmlhr.js"></script>
Code:
<?= javascript_include_tag("common.javascript", "/elsewhere/cools") ?>
Output:
<script type="text/javascript" src="/javascripts/common.javascript"></script>
<script type="text/javascript" src="/elsewhere/cools.js"></script>
Code:
<?= javascript_include_tag("defaults") ?>
Output:
<script type="text/javascript" src="/javascripts/prototype.js"></script>
<script type="text/javascript" src="/javascripts/effects.js"></script>
...
<script type="text/javascript" src="/javascripts/application.js"></script> *see below
Note
If there’s an "application.js" file in your "public/javascripts" directory, javascript_include_tag("defaults") will automatically include it. This file facilitates the inclusion of small snippets of JavaScript code, along the lines of "controllers/application.php" and "helpers/application_helper.php".
