views:

1683

answers:

3

In a html page we use the head tag to add reference to our external .js files .. we can also include script tags in the body .. But how do we include our external .js file in a web user control?

After little googling I got this. It works but is this the only way?

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "MyUniquekey", @"<script src=""myJsFile.js"" type=""text/javascript""></script>", false);

-- Zuhaib

+1  A: 

You can also use

Page.ClientScript.RegisterClientScriptInclude("key", "path/to/script.js");

That's the way I always do it anyway

Phil Jenkins
A: 

Yes this works too .. but why does all the script gets dumped in the body and not in the head??

Zuhaib
you want the script in the body to allow other items in the dom to be loaded first if you reference any of them. browsers execute the javascript right when they're referenced, so having them in the header will make them execute before the rest of the page is available.that being said, there are ways to tie the JS to the event that fires when the page is ready.
Bernard Chen
A: 

Yes this works too .. but why does all the script gets dumped in the body and not in the head??

There's a potential workaround for that here

Phil Jenkins