I don\'t necessarily need to run it at server, however, I would like to use the ~/js/somefile.js
syntax.
Previously, I had just set everything with Absolute
You can use the ScriptManager for this:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/somefile.js" />
</Scripts>
</asp:ScriptManager>
You can get fully what you want by wrapping script tag with asp:ContentPlaceHolder and the you can access it from code behind, for example set will it be executed or not by setting visible property to true or false. Here is the example:
<asp:ContentPlaceHolder runat="server" ID="PrintPreviewBlock" Visible="false">
<script id="PrintPageCall" type="text/javascript" >
$(function() {
window.print();
});
</script>
</asp:ContentPlaceHolder>
and from code behind:
PrintPreviewBlock.Visible = true;
What I've always done is use a normal script
tag and put the src
in <% %>
tags, as illustrated here:
<script language="javascript" src='<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>' type='text/javascript'></script>
You can use functions inside the path string, though, e.g.
<script type="text/javascript"
src="<%=Url.Content("~/Scripts/jquery-1.4.2.min.js") %>"></script>
However that's the ASP.NET MVC syntax for local paths - I can't remember the forms version off the top of my head.
Taken from dailycoding.com:
<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script>