Re: including CSS & JS on only pages that need that CSS & JS [message #175613 is a reply to message #175607] |
Tue, 11 October 2011 12:11 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
Denis McMahon wrote:
> 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,
That is the way of an ignorant. Instead of trying to understand the
reasoning in the arguments, and either challenge it, or act and recommend
accordingly, you are simply ignoring it as if that would change anything.
> is as follows:
It is incorrect in that it is hard to maintain and comparably inefficient.
First of all, you should not use consecutive `echo' statements where simply
leaving (or not entering) PHP mode suffices. In essence, not
<?php
echo "foo";
?>
but
foo
or
<?php
…
?>
foo
<?php
…
?>
> 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.
I accept your premise, I reject your conclusion.
> 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";
That makes one request for each file. I have already explained why that
approach is unwise at best, what a better approach is and why.
> // 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";
One request for each file, the content included verbatim, and XHTML syntax
with no evidence that XHTML is required. One can hardly do worse.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
|
|
|