External JS file in web user control?

和自甴很熟 提交于 2019-12-11 10:47:38

问题


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:


You can also use

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

That's the way I always do it anyway




回答2:


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




回答3:


That's kind of incorrect .. about needing the Javascript in the body.

A Javascript function would sit in the head unexecuted and not at all affected by the fact that elements are still to load.

Often there is a single call at the end of a page to start execution of scripts at the top. (Other methods available)

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



来源:https://stackoverflow.com/questions/280201/external-js-file-in-web-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!