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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With