Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding style and script tags to body acceptable?

I'm creating an AJAX heavy web application and I'm curious on what people's thoughts are on adding style and script tags to the body of an HTML document.

I've been using the jquery.load() method to insert content on the fly. An example partial HTML page that could get loaded into the body element is this:

<script type="text/javascript">
    $(function() {
        // All required java script for the control goes here...
    });
</script>

<style type="text/css">
    /* All required styles for the inserted HTML go here */
</style>

<div id="some-control">
    <!-- Required HTML markup is here. -->
</div>

Since this HTML is getting loaded into a DIV, we are ending up with script and style tags that are not in the head but in the body of an HTML document. I'm not sure if this is valid HTML? Is this a common approach to developing AJAX enabled web applications? Are there any drawbacks I should be aware of?

like image 431
Brian DiCasa Avatar asked Jan 09 '11 17:01

Brian DiCasa


2 Answers

As far as Javascript is concerend, you can put it any where on the page provided that elements it will work upon are loaded before and it does not throw the error undefined element. Famous Yahoo performance article and even Google (in terms of SEO) suggests to put javascript at the end of the page just before the </body> tag.

If you can manage to put your script just before </body> tag, that is considered good approach otherwise what you are doing now should be fine if everything is working properly for you.

like image 90
Sarfraz Avatar answered Sep 25 '22 01:09

Sarfraz


I would actually disagree with Sarfraz and say that you should avoid using <script> and <style> tags in your page body as much as possible. The advantages of moving your JS to an external file are endless. The most obvious include:

  1. leveraging on browser caching - if you just write code in your body, that's extra kilobytes of data that need to be loaded for every page. If it's a universal function, you're wasting precious load time. If it were in an external file, most modern browsers cache that file and only request a new version so often. This decreases server load as well (less requests)
  2. Furthermore, if you ARE using a similar script on multiple pages, what happens if you need to make a change :(. Now you're running around searching for every instance of a <script> tag to make a change. Having all your code centrally located and universal allows for ONE change and DONE
  3. Versioning - if you use version control (GIT, SVN, etc), it's much easier to track and revert one file (if you made a mistake or accidentally lost code) than all of them

CSS share a similar story. Again, with caching and centralized storage, and reusability. It's even more important, however, for styles to match on a website. From a UI standpoint, you don't want your fonts changing from page-to-page and you don't want to edit 40 pages every time you want to add a new style.

As far as having the JS in the document because you are using AJAX loaded content, I suggest you look into .bind and .live. They let you attach handlers to existing and future instances of a DOMObject. For example:

$('.class').live('click', function(){
  alert('I was clicked!');
});

This will apply to any object that existed at page load AND objects that are later created. The following code will NOT - it only applies to objects created on load:

$('.class').click(function(){
  alert('I was clicked!');
});
like image 27
sethvargo Avatar answered Sep 26 '22 01:09

sethvargo