Welcome Guest, Not a member yet? Register   Sign In
JS and CSS as a php file
#1

[eluser]Mareshal[/eluser]
I saw on a website(can't remember which). They were using css and javascript from php files.

something like this:
Code:
[removed][removed]
<link type="text/css" rel="stylesheet" href="<?=base_url()?>css/validator.php" />


but when tried to access them I saw nothing but a blank page. So script was commented, but how? I tried several methods but nothing. any idea?
#2

[eluser]umefarooq[/eluser]
yaa most of time people are using js or css file as php they want to compress or minifiy css and js file for that you have to echo you css or js data in that php file. after compressing or minify. if you are using in CI there some good asset libraries which are giving you these features without using .php extension
#3

[eluser]asylmottaket[/eluser]
Hello

Put this besides the javascript-files, and include the names of the scripts in the array

You should probably minify each javascript-file

Code:
<?php
ob_start( 'ob_gzhandler' );
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
foreach( array(
            'jquery.js',
            'jquery.form.js',
            'config.admin.js') as $fn )
{
    echo file_get_contents($fn);
    echo "\n\n";
}
ob_end_flush();
?>
#4

[eluser]slowgary[/eluser]
The browser doesn't know or care what extensions are on your JS/CSS files, so you can definitely do it. If it just so happens that your files have PHP extensions, that's okay too. Start by just renaming your JS/CSS files to ".php", as long as the content inside each file is valid JS or CSS, you'll have no problems. Then you can use PHP to echo valid JS or CSS into the respective files. The PHP must echo VALID JS or CSS. So:
Code:
< script type='text/javascript' src='/js/script.php' >< /script >
Then in script.php:
Code:
<?php
$username = "Bob Dole";

//this javascript uses the jQuery framework
echo <<< JAVASCRIPT
$(document).ready(function(){
     alert('Welcome, $username');
}
JAVASCRIPT;

?&gt;
#5

[eluser]umefarooq[/eluser]
@slowgary
put first line of you file header('Content-type: text/javascript'); it will work fine
#6

[eluser]skunkbad[/eluser]
You should consider that IE6 can't use a compressed CSS file, and a compressed js file may have similar issues. If you're using output compression with php, you will need to turn it off. I was trying an experiment with serving up css via php, and ran into this issue, and in the end I just went back to using plain css files. If you don't care about IE6 users, then by all means, proceed.
#7

[eluser]Mareshal[/eluser]
[quote author="skunkbad" date="1247108322"]You should consider that IE6 can't use a compressed CSS file.[/quote]

My last site, and my biggest one(for me) had somewhere around 3500 unique visitors per day and around 12-20k page impressions. And If I remember well, only 2% from all visitors have used IE6, and most of all had FF2+(around 78%), so why care about IE 6?

And like a plus most of visitors have had wide screens.
#8

[eluser]slowgary[/eluser]
Yes but YOUR visitors do not represent the entire internet. What type of site is it? The number of IE6 users will vary greatly between browsers. I think you'd find that a site catering to PC First Person Shooter games will have very few IE6 users as they need a new and fast computer to play those games. One the other hand, a site selling life insurance is going to have quite a few elderly people who do not care to buy a new computer every 10 years, so they're still using their windows 9x box with IE6. Each site should make the decision to support IE6 based on their OWN traffic, not on the traffic of another site.
#9

[eluser]Mareshal[/eluser]
Honestly I don't care about IE6, since firefox 2 is free and runs on every computer. Is so hard to click on a download button and spend 10 seconds to install firefox?
#10

[eluser]Dam1an[/eluser]
[quote author="Mareshal" date="1247171496"]Honestly I don't care about IE6, since firefox 2 is free and runs on every computer. Is so hard to click on a download button and spend 10 seconds to install firefox?[/quote]

If it was really that easy, why are so many people still using IE6 then?




Theme © iAndrew 2016 - Forum software by © MyBB