0

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??

2
  • <script type="text/javascript" src="JS/VerticalMenu.js"></script> is how I am adding my script in head Commented Apr 6, 2012 at 15:54
  • try using a slash before JS like: <script type="text/javascript" src="/JS/VerticalMenu.js"></script> Commented Apr 6, 2012 at 15:56

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I'd forgotten about the <base> tag. That is a good solution, too.
1

Try this:

<script type="text/javascript" 
    src="<%=Page.ResolveUrl("~/JS/VerticalMenu.js")%>"></script>

1 Comment

Sounds like a problem with the Javascript file. That would probably be a separate question.
0

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>

11 Comments

That doesn't work if the application is in a virtual folder, like http://www.mysite.com/MyApp/.
I believe ~/ should resolve to the root of the application, even if it's in a virtual directory.
No. ~/ is not working. The path gets appended something like:localhost:50089/Tutorial_Pages/Physics/~/JS/VerticalMenu.js
Is this an ASP.NET Web Site Project or an ASP.NET Web Application Project? There's an important distinction.
Its an asp.net web application
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.