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 #175607 is a reply to message #175565] Tue, 11 October 2011 09:09 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
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.

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";

// 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";

// echo any other html "head" contents here

echo "</head>\n";

?>

Rgds

Denis McMahon
[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: Tue Nov 12 21:52:14 GMT 2024

Total time taken to generate the page: 0.04626 seconds