PDA

View Full Version : where should I place the javascript function definations?



skywind
11-01-2006, 12:56 AM
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!

djr33
11-01-2006, 01:35 AM
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.

jscheuer1
11-01-2006, 06:01 AM
I almost always organize my head like so:


<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.

skywind
11-01-2006, 05:24 PM
<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.

Twey
11-01-2006, 06:02 PM
<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 (http://validator.w3.org/) 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.

mwinter
11-01-2006, 06:24 PM
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