Re: including CSS & JS on only pages that need that CSS & JS [message #175607 is a reply to message #175565] |
Tue, 11 October 2011 09:09 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Sat, 08 Oct 2011 23:51:10 -0700, paris2venice wrote:
> I'm still a newbie at PHP. What would be the right way to include CSS
> and Javascript such that I could add CSS and JS just for those pages
> that need it?
The way I would do it, which doubtless various other people will say is
incorrect for various reasons, is as follows:
Firstly, separate out the css and js into individual files. It's much
easier to maintain the js and css in separate files than it is
encapsulated inside php.
Name each such file according to the styles or functions it contains,
then use code something like this:
<?php
// echo the doctype and opening html tag here
echo "<head>\n";
// echo any meta headers eg content-type here
echo "<title>{$pagetitle}</title>\n";
// then for each javascript file, either:
if (some_condition) echo "<script type='text/javascript'
src='{$jsfilenamex}'></script>\n";
// or:
if (some_condition) echo "<script type='text/javascript'
src='{$jsfilenamex}' />\n";
// and for each css file, either:
if (some_condition) echo "<link href='{$cssfileurlx}' rel='stylesheet'
type='text/css'>\n";
// or:
if (some_condition) echo "<link href='{$cssfileurlx}' rel='stylesheet'
type='text/css' />\n";
// echo any other html "head" contents here
echo "</head>\n";
?>
Rgds
Denis McMahon
|
|
|