Re: PHP attaching css and JS files to current page

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Nov 30, 2008 at 8:23 AM, Alain Roger <raf.news@xxxxxxxxx> wrote:
> Hi,
>
> is there a way how a PHP class can attach JS (javascript) and CSS documents
> to current web page in which the class is instanced ?
> till now i used an "echo" which "write" a <script></script> code into
> current document.
> in this javascript, i used to have a createElement function to attache
> C\other CSS or JS file.
>
> it's not so clean and maybe a better possibility exists. Thanks to let me
> know.
>
> --
> Alain
> -----------------------------------------------------------
> Windows XP x64 SP2
> PostgreSQL 8.3.5 / MS SQL server 2005
> Apache 2.2.10
> PHP 5.2.6
> C# 2005-2008
>

I've created a few helper classes that I use on projects for this
"problem."  What I do is have a directory that I put css and js files
into.  Then I have a main "page" class that has various sub-classes
for things like css & javascript.  Inside of a controller or view if I
have some code that needs to include a js/css file all they have to do
is something like this:

$page = page::getInstance();
$page->css->add('css.css')->add('css2.css');
$page->js->add('file.js')->add('file2.js')

By calling the add() methods page maps to that the specific js and css
directories.

Then in my main site wrapper template I call upon page again to render
out any css/js files that had been "included."  This way my design has
no idea of what js/css that needs to be included and anywhere along
the execution path of my code I can add css/js.

This also allows for some speed increases in page load since you can
get your css included in the header while delaying script includes
until way down in the body.  This prevents browsers from blocking
while compiling your scripts and the user sees something a lot faster.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux