#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Ljubljana, Slovenia, Europe
    Posts
    29
    Rep Power
    0

    JavaScript without HEAD


    Is it possible to write JavaScript code without inserting fuctions into HEAD part of the page?

    I generate my pages dinamicly. In PHP a always use the same file to generate header of the page. How can I then use JavaScript if I can't have JavaScript code in the HEAD section of generated HTML?

    I really have no idea about JavaScript, sorry.

    Blaz
  2. #2
  3. yet another member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    262
    Rep Power
    14
    Hi!

    <script> blocks are allowed within <body> and </body>. Code in one of those blocks that is not encapsulated in functions is immediately executed after being loaded in the browser. So you can use functions here as well.

    Atrus.
    Webmaster - Stefan Meier KG TABAKWAREN - Pfeifen, Premium-Zigarren, ... (_Ger)
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    HTML event handlers (onClick,onMouseover, etc.) are JavaScript (so, of course, is the javascript&#58; url) and are found in the <body> of the document. You can reference functions in them, but also embed the JS right in the HTML.

    You can also do this:

    <body>
    <table>
    ......etc.
    </table>
    <div>content</div>

    <script language="JavaScript" type="text/javascript" src="myScript.js">

    <....more markup...

    where myScript.js is an external JavaScript file with nothing but JS in it. It will be written into the HTML stream right where it appears, as if it was hardcoded there. More...
  6. #4
  7. yet another member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    262
    Rep Power
    14
    Hi Adios!

    Thanks for your post, I did not know that, yet! Would you confirm one other thing?! I've seen this before and it works, but is it valid, too? Simply having JS block within the body?

    <body>
    <table>
    ......etc.
    </table>
    <div>content</div>

    <script language="JavaScript" type="text/javascript">

    <!--
    function doingStuff { etc.....}
    var hello = "Hello World";
    document.println(hello);
    //-->

    </script>

    <....more markup...
    Webmaster - Stefan Meier KG TABAKWAREN - Pfeifen, Premium-Zigarren, ... (_Ger)
  8. #5
  9. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Yes.

IMN logo majestic logo threadwatch logo seochat tools logo