Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3389

Re: including CSS & JS on only pages that need that CSS & JS

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: including CSS & JS on only pages that need that CSS & JS
Date 2011-10-11 09:08 -0400
Organization A noiseless patient Spider
Message-ID <j71f50$b2q$1@dont-email.me> (permalink)
References <e3d8f6c1-a54c-436b-930e-44dd89997f12@v18g2000pri.googlegroups.com> <4e9407bd$0$28439$a8266bb1@newsreader.readnews.com>

Show all headers | View raw


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@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-08 23:51 -0700
  Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 08:34 -0400
    Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-10 02:22 -0700
      Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 08:27 -0400
  Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-09 08:46 -0500
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 13:52 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Luuk <Luuk@invalid.lan> - 2011-10-09 20:03 +0200
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 15:08 -0400
  Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-09 16:31 -0400
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 19:54 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 20:03 -0400
      Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-09 22:43 -0500
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 08:30 -0400
          Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-10 13:04 -0400
            Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 13:44 -0400
              Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-10 21:35 -0500
                Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 23:04 -0400
                Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-10 22:14 -0500
                Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-10 23:24 -0400
                Re: including CSS & JS on only pages that need that CSS & JS Tim Streater <timstreater@greenbee.net> - 2011-10-11 10:33 +0100
                Re: including CSS & JS on only pages that need that CSS & JS The Natural Philosopher <tnp@invalid.invalid> - 2011-10-11 11:29 +0100
                Re: including CSS & JS on only pages that need that CSS & JS Tim Streater <timstreater@greenbee.net> - 2011-10-11 12:00 +0100
            Re: including CSS & JS on only pages that need that CSS & JS The Natural Philosopher <tnp@invalid.invalid> - 2011-10-10 19:34 +0100
              Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 15:16 -0400
        Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-10 23:17 +0200
          Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 17:36 -0400
  Re: including CSS & JS on only pages that need that CSS & JS Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-11 09:09 +0000
    Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-11 14:11 +0200
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 09:08 -0400
      Re: including CSS & JS on only pages that need that CSS & JS "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2011-10-11 13:29 +0000
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 13:14 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-11 19:14 +0000
        Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-11 22:38 +0200
      Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-11 17:48 -0700
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 21:51 -0400
          Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-11 22:03 -0700
            Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-12 06:19 -0400
        Re: including CSS & JS on only pages that need that CSS & JS Hans Olo <doctor@vaderlishious.com> - 2011-10-11 22:11 -0500
          Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-12 06:20 -0400

csiph-web