How can I use runat="server" on a script tag in asp.Net

Armstrongest picture Armstrongest · Aug 18, 2010 · Viewed 14.6k times · Source

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.

Answer

David picture David · Aug 18, 2010

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>