Last Updated: February 25, 2016
·
66.53K
· petermorlion

Relative paths from the root in javascript

In ASP.NET you can use relative paths from the root of your domain by using the tilde character:

"~/Scripts/myScript.js"

In javascript, people sometimes wonder how to do this. We don't wan't to hard-code our domain, but we also don't want to use the './' notation, as our files could move around later. This would break things, and we might not know it.

One trick is to use '../../..', but you're never quite sure how many levels you need to move up.

The solutions is quite simple:

'/Scripts/myScript.js'

Starting with a '/' will create a relative path starting from your root.