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 #175577 is a reply to message #175576] Sun, 09 October 2011 23:54 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 10/9/2011 4:31 PM, Michael Joel wrote:
> On Sat, 8 Oct 2011 23:51:10 -0700 (PDT), paris2venice
> <paris2venice(at)gmail(dot)com> 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?
>>
>> Would it be something like this? Is this how you do it?
>>
>> <?
>> php require_once './css.php'; /* css.php contains
>> doctype, meta& css needed universally */
>> ?>
>>
>> <!-- link href= calls to CSS needed on local page only -->
>>
>> <?
>> php require_once './js.php'; /* js.php contains
>> javascript calls needed universally */
>> ?>
>>
>> <!-- script type="text/javascript" calls to Javascript needed on
>> local page only -->
>> </head>
>>
>>
>> Thanks a bunch for your help.
>
>
> I think you are meaning you want to use PHP includes to drop in your
> css and javascript. You can use the normal method combines with PHP or
> just PHP...
>
> Combined methode:
>
> <link rel="stylesheet" type="text/css" href="GlobalCSS.css" />
> <script type="text/javascript" src="GlobalJavascript.js"></script>
> <?php
>
> if($MYpageID="THIS IS THE PAGE I WANT IT ON") {
> include("StylesForThisSpecialPage.php");
> include("StylesForThisSpecialPage.php");
> }
> ?>
>
> The other method:
>
> <?php
> include("GlobalCSS.php");
> include("Globalavascript.php");
>
> if($MYpageID="THIS IS THE PAGE I WANT IT ON") {
> include("StylesForThisSpecialPage.php");
> include("SpecialJavascript.php");
> }
> ?>
>
> Of course the PHP files must be written just as they would be if they
> were coded directly on the HTML page (not as if they are being
> "linked").
> Right or wrong? It works.
>
> Mike

None of which are a good idea. And so those who keep espousing these
crappy solutions (which are off topic in this newsgroup but there are so
many of them I will respond anyway):

CSS files should be CSS files - and should not have a .php extension;
there is almost never a need for any php code in a css file. The same
with javascript and .js extensions.

And files should be included using link tags, not directly included in
the source file directly (unless they are specific to this page - in
which case there is no need for conditional includes). That way file
can be cached by the browser and/or routers between the web server and
the client, decreasing both network traffic and server load.

There are a lot of things "that work" but are not a good idea.

--
==================
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: Sun Oct 20 19:20:16 GMT 2024

Total time taken to generate the page: 0.04869 seconds