SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Jed.
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add javascript in the middle of the html file

    Can I add javascripts in the middle of my page or it should be btw <head></head>?

    Code:
    .
    .html codes
    .
    <script src=scripts.js language="javascript" type="text/javascript"></script>
    .
    .html codes
    .
    .

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To be as unobtrusive as possible:


    <script type="text/javascript" src="external.js"></script>
    </head><body>

    And then put all the necessary javascript code into its own separate external.js file.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I add javascripts in the middle of my page
    You can, but you shouldn't. The idea is to keep the javascript(called the behavior) separate from the html(called the structure), and to keep the css(called the presentation) separate as well. It makes maintenance(i.e. the ability to change things) easier.

    The best practice is to link to a javascript file using script tags in the <head> by using a src attribute in the script tags. The next best practice is to put the javascript directly in the <head>.
    Last edited by 7stud; Feb 22, 2007 at 05:04.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can put them (with src=) anywhere you like. But lose the 'language=' attribute-
    the only browsers that use 'language' will download javascript files without it.
    They only pay attenton if a version number is specified.
    Last edited by mrhoo; Feb 22, 2007 at 01:12.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •