External JS file in web user control?

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

After a 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


source to share


3 answers


You can also use

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

      



That I always do it anyway

+2


source


Yes it works too .. but why does the whole script get dumped in the body and not in the head?



There is a potential workaround for here

+1


source


So wrong .. about the need to use Javascript in the body.

The Javascript function will sit in your head unfulfilled and not at all affected by the fact that the elements are still loading.

Often there is one call at the end of the page to start executing the scripts at the top. (Other methods available)

The only reason the script gets dumped into the body is because MS doesn't seem to give 2 hoots about JavaScript. Shame, because forcing us all to use .NET just makes the world full of sluggish pages that uselessly run back and forth to servers for minimal requirmenet.

+1


source







All Articles