Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net - relative url for javascript src?

Tags:

.net

I want to include a javascript file like so

<script type="text/javascript" runat="server" src="~/file.js"></script>

The idea is that I want to include the file.js via relative path instead of an absolute path, and I want .NET to figure out the appropriate directory (ie. is it ./ or ../, or ../../ etc...).

But when I try the code above, I get compilation error.

HOw do I properly include a javascript file?

like image 228
John Avatar asked Oct 28 '25 04:10

John


1 Answers

You can try this...

Razor:

<script type="text/javascript" src="@Url.Content("~/file.js")"></script>

WebForms:

<script type="text/javascript" src="<%=Page.ResolveUrl("~/file.js")%>"></script>
like image 70
Brian Avatar answered Oct 31 '25 08:10

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!