I have done enough research and have decided to move the JavaScript to the bottom of the page.
Here is why i should do it http://developer.yahoo.com/performance/rules.html#js_bottom
I wrote the code below, this code only can add the JavaScript within the header.
How do I add the JavaScript before the </body> of the page? I am thinking about using
The code below will adding the JavaScript to the header not the body.
public static string AddJavascript(string filename, Page ThisPage)
{
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = filename;
ThisPage.Header.Controls.Add(js);
//Add to bottom of the page.
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
AddJavascript("myjd.js", Page);
}
I like to produce something like below this dynamically. I have several JavaScript files so i really like to keep them separate.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
</title>
</head>
<body>
<div>
my content here
</div>
<!---javascript should go below here-->
<script type="text/javascript" src="myjd.js">
</script>
</body>
</html>
Use
ThisPage.ClientScript.RegisterStartupScript(this.GetType(), "myjd", "[script]", true)to register it at the end (just before closing offormtag).