|
home—lects—exams—hws
breeze (snow day)
Suppose
main.php includes
header.php which needs
main-validate.js which uses
utils.js.
But,
main.php also requires utils.js in its own right.
(Moreover, other other .php files will
include javascript which includes utils.js.)
How do we handle multiple includes?
Nest includes:
A's code would just
However, if A itself needed some of
the functions inside B,
it would re-include that file.
Advantage: simple to maintain — if you want to use a function defined in my-utils, you only need to require that one file, and you don't have to worry about what pre-requisites my-utils has.
Disadvantage: Underlying files may get included many times (perhaps you require files D, E, and F, and each of those requires my-utils.
Here is an example where require-master.php requires both require-util.php and require-helper.php ; in turn, require-helper.php also requires require-util.php.
A more difficult disadvantage:
You can't always use relative paths:
if
require-master.php does
a
Alternately,
Don't nest any includes;
if file A requires B which requires C,
then A's code
would
Advantage: No file ever gets included more than once.
Disadvantage: The programmer needs to be aware of
(or frequently re-read documentation of)
which other files include'd file needs
// This is the file my-utils.h: #ifndef MY_UTILS_H // if the variable is *not* defined, then proceed: #define MY_UTILS_H // Define the variable, so next time the `ifndef` will be false. ⋮ // actual code #endif |
Note that the named constant
require( |
First, just be sure to remember that the html tag “
<script type="text/javascript">alert("inline javascript");</script> <script type="text/javascript" src="someOtherFile.js"></script> <-- NOTE: You can't use the ".../>" empty-tag abbreviation with tags like script, p, ... --> |
But what if you're inside javascript
(say, inside a .js file)
and you want to include a different file?
Javascript does not have a native
function include(filename) { var scriptTag = document.createElement('script'); scriptTag.src = filename; scriptTag.type = 'text/javascript'; var head = document.getElementsByTagName('head')[0]; head.appendChild(scriptTag) }2 |
function include(filename) { document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); }3 |
What if you include the same javascript file several times? Note that this could incur a network-fetch penalty. Ideally the browser would just use a cached copy, but unless the server provides an expiration date in the http response header (which it may not be configured for), the browser technically should re-fetch.
And even if a local cache is used, we still run into problems
if there are global variables
that get re-initialized.
We can fix this ourselves,
either manually using the ol' C pre-processor trick of defining a variable for each file,
or write our own
1Of course, if the file contains content — like a navbar — that you want to appear multiple times, then you'd avoid the “_once” version. ↩
2Taken from showthread.php?t=146094 ↩
3Taken from script17_include_js_from_js.htm ↩
home—lects—exams—hws
breeze (snow day)
©2011, Ian Barland, Radford University Last modified 2011.Oct.12 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |