16

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.

Pops
  • 30,199
  • 37
  • 136
  • 151
Armstrongest
  • 15,181
  • 13
  • 67
  • 106

5 Answers5

20

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>
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
David
  • 208,112
  • 36
  • 198
  • 279
  • Thanx! Yes, now I remember seeing this before. – Armstrongest Aug 18 '10 at 19:07
  • 1
    Just an FYI, according this post: http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blo it's better to use # (DataBinding Expression) and Bind from behind. – Armstrongest Aug 18 '10 at 19:38
  • @dl-_-lb: Never saw that before, but it makes sense. Thanks! – David Aug 18 '10 at 20:01
  • 5
    For some, an additional tip that might help if they get the "Controls collection cannot be modified" exception is to wrap the above method in a tag. – Timothy Lee Russell Apr 18 '14 at 05:24
14

You can use the ScriptManager for this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/js/somefile.js" />
        </Scripts>
</asp:ScriptManager>
Jemes
  • 2,822
  • 21
  • 22
5

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;
jasin_89
  • 1,993
  • 7
  • 30
  • 41
  • This only works if you're putting it in a `.master` file; you can't do this in a regular aspx web page. – Mar Dec 13 '18 at 19:09
3

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.

Rup
  • 33,765
  • 9
  • 83
  • 112
  • forms version is the same. MVC syntax is inherited from web forms. – Joel Coehoorn Aug 18 '10 at 19:02
  • @Joel I meant the `UrlHelper` class that I'm using is MVC-only. As the others beat me to, the forms version is `ResolveUrl` on `System.Web.UI.Control`. – Rup Aug 18 '10 at 19:27
1

Taken from dailycoding.com:

<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script> 
palswim
  • 11,856
  • 6
  • 53
  • 77