Last Updated: February 25, 2016
·
67.81K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside