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


Groups > comp.lang.php > #3388

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1712757.iOTu7YrNUb@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Tue, 11 Oct 2011 14:11:20 +0200
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
Subject Re: including CSS & JS on only pages that need that CSS & JS
Newsgroups comp.lang.php
References <e3d8f6c1-a54c-436b-930e-44dd89997f12@v18g2000pri.googlegroups.com> <4e9407bd$0$28439$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.php
MIME-Version 1.0
Lines 90
NNTP-Posting-Date 11 Oct 2011 14:11:20 CEST
NNTP-Posting-Host cd4cab9d.newsspool1.arcor-online.net
X-Trace DXC=ee?70Z4FER0T2Rfi6<W9f5ic==]BZ:af>4Fo<]lROoR1<`=YMgDjhg2E4CgK@@nP?<DZm8W4\YJN<b@mF9jNikd5_dXMBNGELE1@60;OSYGb:3
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.php:3388

Followups directed to: comp.lang.php

Show key headers only | View raw


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.)

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