Results 1 to 6 of 6

Thread: where should I place the javascript function definations?

  1. #1
    Join Date
    Nov 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default where should I place the javascript function definations?

    where should I place the javascript function definations?

    I put fade in slide show javascript function defination after <head> before <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <head>
    <script type="text/javascript">
    ...
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    When I run the page in firefox. It makes the browser revisit the server again. (I use the php code to record the click, one click to this page record two clicks) But if I run it in Internet Explorer, it is fine.

    My guess is that firefox default set up as display while downloading, internet explorer set up as display after full download.

    But I cannot explain why javascript put before meta tags caused the firefox browser visist the server twice.

    But if I put the javascript block after <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> , it is fine. It will not revisit the server.

    What is the rule here about where to place the javascript block?

    I understand that put javascript after meta tags is good practice. But I want to know why if javascript put before meta tags, it makes firefox visist the server twice. Now, it only messed up my stats record, but itt could cause logic error in the other programming issues, such as sumbit the online orders twice etc.

    I searched around, but no authentic answer about why put javascript block before <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> cause this error.

    Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    JS should be able to go anywhere within the head section. (And sometimes after that.)
    Link to your page if you need specific help with it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I almost always organize my head like so:

    HTML Code:
    <head>
    <title>Title Here</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    any other meta tag(s)
    stylesheet(s) and/or style link(s)
    script(s) and/or external script tag(s)
    </head>
    except when editing other's work, in that case, sometimes I don't bother, especially if most of the rest of their work exhibits non-standard organization. I'm not certain that my method is necessarily the best but, it validates, whereas some other ways of ordering these items will not. Give the above a try and see if it helps.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Yes. I did that too and it solved the problem. But I cannot figure out why.

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    has to be before the <script language="javascript></script>

    I cleaned off all the codes, only keep a blank html page with the head and body. plus very simple php codes to record the clicks.

    And I tried out different orders, for firefox, this <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> has to be before javascript.

    Other meta tags could be placed before or after javascript, php programming doesn't make any difference. But someone pointed out, put javascript behind all meta tags make the page search engine friendly.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <meta> tags have to go before any other elements (except <html> and <head>, obviously). This is because they can contain information that affects how the rest of the page is read. You should validate your page (as HTML 4.01 Strict).
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    An HTTP header is most definitely preferable for this purpose. ISO-8859-1 is the default, if I remember correctly, so that whole <meta> tag is rather redundant, headers or no headers.
    <script language="javascript></script>
    You're missing a quote there, but I presume that was a typo. The language attribute is deprecated for type.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    ISO-8859-1 is the default, if I remember correctly, so that whole <meta> tag is rather redundant, headers or no headers.
    Yes, it is. However, most browsers (at least for Windows) seem to assume a Windows code page (typically Windows-1252) if no encoding information is given and auto-detection doesn't reveal a better choice, therefore it's best to always specify a value.

    Mike

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
  •