INSERT_BRAIN_HERE
INSERT_BRAIN_HERE

Reputation: 37

Where to insert this javascript?

I have the same problem as AndyHin does (CSS HTML - DIV height fill remaining space) but can't see where to insert the javascript when Hussein says to. I must admit that I am quite new to writing in js.

I have tried including it between the head tags as below and I have also tried inserting it into the jquery with no avail.

<head> 

<script> 

Hussein's answer 

</script> 

</head>

Upvotes: 0

Views: 206

Answers (3)

Stefan
Stefan

Reputation: 14863

Put right before the closing body tag (</body>) (best solution), or wrapp all code of the script with

$(function() { Hussein's answer });

If you don't add the script at the end of thedocument or use $(document).ready(), the code gets executed before the dom is ready, therefor your javascript find the dom elements, and does nothing (in the best case)!

Here is in article about $(function() {}) (the short form of $(document).ready())

Upvotes: 1

Anthony Grist
Anthony Grist

Reputation: 38345

It relies on elements on the page, so you need the script execution to be delayed until after the DOM has finished being constructed (so the required elements can actually be selected). You have two options for that:

  1. Place the <script> tag at the end of the body of the HTML document:

    <script>
        // code here
    </script>
    </body>
    
  2. Place the <script> tag inside the <head> tag (or anywhere inside the <body> since it doesn't actually matter), and use a DOM ready event handler:

    <script>
    $(document).ready(function() {
        // code here
    });
    </script>
    

Upvotes: 2

Manoj Pilania
Manoj Pilania

Reputation: 666

Add the following reference in head part

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

You can insert anywhere on the page.

<script>

$(document).ready(function(){
    var one = $('#col_1').height(),
    two = $('#col_2').height(), 
    remaining_height = parseInt($(window).height() - one - two); 
    $('#col_3').height(remaining_height); 
});

<script>

Upvotes: 0

Related Questions