Re: including CSS & JS on only pages that need that CSS & JS [message #175620 is a reply to message #175614] |
Wed, 12 October 2011 00:48 |
paris2venice
Messages: 9 Registered: October 2011
Karma:
|
Junior Member |
|
|
On Oct 11, 6:08 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 10/11/2011 5:09 AM, 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, 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.
>
> Very definitely. It also increases reusability.
>
>> 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
>
> There is no need to echo these. You can just code them as plain html.
> Easier to read and maintain.
>
>> echo "<title>{$pagetitle}</title>\n";
>
> OK, this may be something good to echo. The real question is - where
> does $pagetitle come from? If it's from a database, then this is good.
> But if you just set it at the top of the script, again, why complicate
> things?
>
>> // 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";
>
> Here you do need PHP for the conditional.
>
>> // 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";
>
> I fail to see the difference between the two.
>
>> // echo any other html "head" contents here
>
>> echo "</head>\n";
>
>> ?>
>
> Again, no need to echo what can be done more simply in plain html.
>
>> Rgds
>
>> Denis McMahon
>
> The only other comment I have is - if you're using the majority of the
> javascript and css code in your pages, why even split them up? Placing
> all you css of javascript in one file can actually cut traffic and
> overhead because the browser will cache the file once and reuse it.
Jerry, I don't get it. What did I say that led you to believe I was
doing anything else except what both you and PointedEars describe
above? I'm not faulting you -- I obviously did not communicate
effectively with the HTML comments I used.
My include files are nothing but pure HTML. Thus, I'm not mixing the
DOCTYPE with CSS and never have. I've been creating websites almost
since the inception of the web (1995) so I do know what I'm doing and
I certainly don't need to read a book on HTML or CSS. PHP? Yes. But
if what I asked was so simple, there wouldn't be 33 posts already.
Thanks very much to everyone who contributed. I sincerely appreciate
your efforts to help.
|
|
|