FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » including CSS & JS on only pages that need that CSS & JS
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: including CSS & JS on only pages that need that CSS & JS [message #175614 is a reply to message #175607] Tue, 11 October 2011 13:08 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
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.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: .htaccess vs PHP header(location:)
Next Topic: How to redirect user based on user country?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 23:08:29 GMT 2024

Total time taken to generate the page: 0.07305 seconds