2

I have an ASP.NET MVC app that will be hosted within an existing URL structure. For that reason, I want to make my URLs relative. In an ASP.NET MVC, I would do something like the following:

<img src="~/images/logo.png" />

From my understanding, the tilde is an ASP.NET MVC thing that gets rendered at runtime. However, I have some JavaScript files that are static. I need them to reference something relatively.

How could I do ~/images/logo.png in JavaScript?

2 Answers 2

1

~ refers to application root folder

in case your application is not hosted in a sub folder like http://example.com/mydir/myapp (in nested virtual directory) then the javascript equivalent will be /images/logo.png

Sign up to request clarification or add additional context in comments.

Comments

0

The ~ symbol as an ASP.Net feature that is resolved server-side -- Javascript does not know anything about it. The only way to do this from Javascript, is to target relative to the path where the current file is served.

So if your path structure is like this:

Web Application
  Content
    js
      test.js
    images
      logo.png

Then, from your "test.js" file, you would reference "logo.png" as ../images/logo.png.


(Technically, you could write a dynamic Javascript file with server tags, but that seems like an unnecessary measure just to resolve a relative path.)

Comments

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.