Tuesday, February 8, 2011

Load a Script file from any Location

My organization is developing an ASP.NET application consisting of a parent WAP project with multiple WAP sub-projects.  I ran into a problem loading JavaScript files located in the parent WAP Scripts folder – the sub-project WAPs were not able to load the scripts.

There are two solutions to this problem.  One answer is to use a declarative path in the script tag:

  1. <script type="text/javascript" language="javascript" src="<%=ResolveUrl("~/Scripts/MyScript.js")%>"></script>

A second answer is to use a ScriptManager (or ScriptManagerProxy) control:

  1. <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
  2.     <Scripts>
  3.         <asp:ScriptReference Path="~/Scripts/MyScript.js" />
  4.     </Scripts>
  5. </asp:ScriptManagerProxy>

Either solution appears to work for any page in the parent or any sub-project WAP.  However, the ScriptManager solution is a more reliable way to ensure that all scripts are loaded in the correct sequence.

No comments:

Post a Comment