Reputation: 15430
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 paths and set my project to be at the root level. SO, I'd just declare all my stylesheets, background images and javascript files something like /css/somefile.css
However, for this project, it doesn't run as root.
I can't put runat="server"
on a script tag.
I can put it on a link tag, though.
This must be a common problem with a few simple answers.
Upvotes: 16
Views: 16504
Reputation: 2001
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;
Upvotes: 5
Reputation: 219127
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>
Upvotes: 20
Reputation: 2842
You can use the ScriptManager for this:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/somefile.js" />
</Scripts>
</asp:ScriptManager>
Upvotes: 14
Reputation: 12140
Taken from dailycoding.com:
<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script>
Upvotes: 1
Reputation: 34418
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.
Upvotes: 3