Javascode in header?

Discussion in 'JavaScript' started by Johnburk, Feb 12, 2006.

  1. #1
    I found a java code for a dropdown menu and they want me to put it in my header.

    <script language="JavaScript"><!--
    
    function goThere(form){
    	var linkList=form.selectThis.selectedIndex
    	if(!linkList==""){window.location.href=form.selectThis.options[linkList].value;}
    }
    //--></script>
    Code (markup):
    Is there another option of using a java code instead of putting it in the header
     
    Johnburk, Feb 12, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want function/variables to be available anywhere in the document, put them between the <head> tags. But you can put Javascript in <script> tags anywhere within the body tags.

    BTW, that is Javascript, not Java (Two very different things.)
     
    exam, Feb 12, 2006 IP
    Johnburk and AWD1 like this.
  3. AWD1

    AWD1 Peon

    Messages:
    191
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In an external Javascript document called from the header. It sounds the same, but the Javascript won't get parsed by a spider, which makes your code more readable both from a search engine standpoint and a webmaster standpoint.

    If you externalize your Javascript, you can call it from anywhere you want. You just won't be able to call your functions until after you call your JScript (hence the reason putting it in the <head></head> area is a good thing.) Something like this:
    
    <head>
    <script type="text/javascript" src="your_script_here.js">
    <!--
    //-->
    </script>
    </head>
    ...
    ...
    ...
    <script type="text/javascript">
    <!--
    someFunctionFromYourScriptAbove();
    //-->
    </script>
    
    Code (markup):
    It's a lot easier to read, once you're used to it.
     
    AWD1, Feb 12, 2006 IP
    Johnburk likes this.
  4. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    put it in an external file, and link to it.

    tom
     
    TommyD, Feb 12, 2006 IP