I have an asp.net application containing pages that lie in multiple folder. I have my.js files also in one "JS" folder and I have added their reference in head of master page like:
<script type="text/javascript" src="JS/jquery.min.js"></script>
Now when I am on home page, the script loads fine. But when I am on some other page that is present in another folder(Physics for example), the path gets appended and hence I get the error:
Failed to load resource: the server responded with a status of 404 (Not Found)
Similar thing is happening for my image paths and <a>
tags also.
Now I want to give paths with respect to root path something like:
~/JS/VerticalMenu.js
But this ~
is not taking me to the root of my application. Do I need to set where ~
should lead to? And if yes then where and how??
The @Charlie Kilian answer is a workable solution however you can also specify a base URL for all the relative URLs in your page by base tag in head of your html page.
<head>
<base href="http://www.yourdomain.com/anyvirtualdirectory/" />
</head>
Try this:
<script type="text/javascript"
src="<%=Page.ResolveUrl("~/JS/VerticalMenu.js")%>"></script>
If it's an ASP.NET application, you can access the root by prefixing the path ~/
:
<script src="~/common/scripts/safeguard.common.js" type="text/javascript"></script>
You can also try prefixing the path with just /
:
<script src="/common/scripts/safeguard.common.js" type="text/javascript"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.