Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I embed CSS/JavaScript files in a web application?

I've recently started embedding JavaScript and CSS files into our common library DLLs to make deployment and versioning a lot simpler. I was just wondering if there is any reason one might want to do the same thing with a web application, or if it's always best to just leave them as regular files in the web application, and only use embedded resources for shared components?

Would there be any advantage to embedding them?

like image 683
Max Schmeling Avatar asked Jul 27 '09 20:07

Max Schmeling


People also ask

Can you embed JavaScript in CSS?

You can, but you really ought not to. Instead, use separate script files which happen to correspond to the CSS files you use.

Why do we need external CSS and JavaScript files?

The first major benefit of using external CSS style sheets and external JavaScript files is faster load times for your web pages. According to an experiment at Google, even a half a second difference in page load time can make a 20% difference in how much traffic that pages retain.

Where should I put my JavaScript files?

To place JavaScript in an HTML file, use the <script>… </script> tag. You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags.

Should I use CSS JavaScript?

If the item requires interaction from the user, use JavaScript (things like hovering, focusing, clicking, etc.). If the item needs to change in visibility, needs to be animated, or have any other visual change made to is, use CSS.


4 Answers

Of course if anyone who knew what they were doing could use the assembly Reflector and extract the JS or CSS. But that would be a heck of a lot more work than just using something like FireBug to get at this information. A regular end user is unlikely to have the desire to go to all of this trouble just to mess with the resources. Anyone who's interested in this type of thing is likely to be a malicious user, not the end user. You have probably got a lot of other problems with regards to security if a user is able to use a tool like the assembly reflector on your DLL because by that point your server's already been compromised. Security was not the factor in my decision for embedding the resources.

The point was to keep users from doing something silly with these resources, like delete them thinking they aren't needed or otherwise tamper with them.

It's also a lot easier to package the application for deployment purposes because there are less files involved.

It's true that the DLL (class library) used by the pages is bigger, but this does not make the pages any bigger. ASP.NET generates the content that needs to be sent down to the client (the browser). There is no more content being sent to the client than what is needed for the page to work. I do not see how the class library helping to serve these pages will have any effect on the size of data being sent between the client and server.

However, Rjlopes has a point, it might be true that the browser is not able to cache embedded JavaScript/CSS resources. I'll have to check it out but I suspect that Rjlopes is correct: the JavaScript/CSS files will have to be downloaded each time a full-page postback is made to the server. If this proves to be true, this performance hit should be a factor in your decision.

I still haven't been able to test the performance differences between using embedded resources, resex, and single files because I've been busy with my on endeavors. Hopefully I'll get to it later today because I am very curious about this and the browser caching point Rjlopes has raised.

like image 112
Frinavale Avatar answered Sep 22 '22 10:09

Frinavale


Reason for embedding: Browsers don't download JavaScript files in parallel. You have a locking condition until the file is downloaded.

Reason against embedding: You may not need all of the JavaScript code. So you could be increasing the bandwidth/processing unnecessarily.

like image 22
easement Avatar answered Sep 20 '22 10:09

easement


Regarding the browser cache, as far as I've noticed, response on WebRecource.axd says "304 not modified". So, I guess, they've been taken from cache.

like image 24
Arthur Avatar answered Sep 24 '22 10:09

Arthur


I had to make this same decision once. The reason I chose to embed my JavaScript/CSS resources into my DLL was to prevent tampering of these files (by curious end users who've purchased my web application) once the application's deployed. Reason against embedding: You may not need all of the JavaScript code. So you could be increasing the bandwidth/processing unnecessarily.

like image 39
user2721863 Avatar answered Sep 20 '22 10:09

user2721863