Jump to content


What's wrong with using in-document CSS and Javascript?

css javascript

3 replies to this topic

#1 404

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationEarth

Posted 21 January 2012 - 11:16 PM

Hey guys,
It recently occured to me that instead of causing unnecessary HTTP requests
with lines like these:

<link rel="stylesheet" href="style.css">
<script src="script.js"> </script>

I could put both the CSS and Javascript into the HTML file, saving the browser
two HTTP requests. So the lines would look like this:

<style>
<?php include("style.css"); ?>
</style>

<script>
<?php include("script.js"); ?>
</script>

Why isn't this widespread? It seems like a pretty smart thing to do.

#2 topdown

    Advanced Member

  • Members
  • PipPipPip
  • 161 posts
  • LocationWisconsin

Posted 22 January 2012 - 12:52 AM

You should not use serverside languages to include static files that users have access to via the browser or anything else.
It opens you up to injections and whatever else they can come up with to hack the site.
Not sure how easy it is to actually pull off, but that would be my reason to avoid it.


After thinking about it I'm not sure how possible the above is even if you got an include to work.
The include itself should fail. It does on my localhost anyway.

If I did anything it would be a tool to minify it on the fly and serve it up.

Let the browser and HTTP do the work intended, just don't ever do it like a lot of sites do.
Eg. Having 10 stylesheets with all of the CSS seperated by use, layout, font, color, etc...

Same with JavaScript.
I tend to develop, then put all js in one file and minify it.
All css in one file and minify it.

Then I just load the 2 minified version on the live sites.
2 requests is nothing.
Some of the most popular sites have 100+ which is terrible and shows when you load the page.

#3 404

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationEarth

Posted 22 January 2012 - 11:15 AM

Ok, but I don't really understand your reason why I shouldn't include() it.

Why would the include fail? I have to admit that I haven't tried it yet, though.

#4 Zoopstud

    Advanced Member

  • Members
  • PipPipPip
  • 176 posts
  • LocationWales, UK

Posted 22 January 2012 - 11:52 AM

Well once you have visited a page it caches the css and js so you don't need to download it again for every page you visit on that site, if you use php to include them you will have to download all the css and js for every single page you visit, may make for a few less http requests but will increase bandwidth.
The more you learn, the more you realize how little you know…