SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hiding javascript for using it in more than 1 page

    Hi,

    How do I hide the javascript code so that it allows me to use the same code on multiple pages without having to add it to each page.

    Thanks,
    John

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you are looking for?

    Put this :
    <script language="JavaScript" src="myscript.js"></script>
    in your <head> section.

  3. #3
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, how simple! Thanks
    By the way, does the hiding slow down the page's loading time?

  4. #4
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont think it will, unless your JS script is of a huge size.

    Simple JS scripts only take a few kilobytes.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by astericks
    Put this :
    <script language="JavaScript" src="myscript.js"></script>
    in your <head> section.
    Well, that's not entirely true. If the code wouldn't normally go in your page's <head>, don't put it in the <head> section. Just put the line where your code would normally be. Also, to make it standards-compliant, add this attribute to the <script> tag:
    Code:
    type="text/javascript"

  6. #6
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by mattjacob

    If the code wouldn't normally go in your page's <head>, don't put it in the <head> section. Just put the line where your code would normally be.
    It seems to me to be more in line with W3C's intentions of promoting clean, well-structured and separated markup/code to have all the javascript 'guts' in external files and have all links to external .js files grouped in the header away from the structural markup.

    That way they don't clutter up the markup unneccessarily.

    If you're using interactive javascripts on the page, then the only thing that actually needs to appear within the body markup is the function calls themselves. (onclick="myFunction();...)

    Anyway, that's just my opinion.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're like me, and you use lots of javascript on your pages, you can define the default scripting language scope with a meta tag
    Code:
    <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript" />
    So you can still call you scripts using regular <script></script> tags and still remain standards compliant!

    Another handy one is for styles as well
    Code:
    <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css" />
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •