Well, i understand my title is a bit confusing. I will state it clearly below with the example.
Have a look at the VirtualPathUtility Class and this information from msdn about ASP.NET project paths.
The VirtualPathUtility.ToAppRelative method is probably what you're looking for.
VirtualPathUtility.ToAppRelative
If the virtual path for the application is "myapp" and the virtual path "/myApp/sub/default.asp" is passed into the ToAppRelative method, the resulting application-relative path is "~/sub/default.aspx".
It explains and gives examples on how to convert between different path formats.
I also think you should to output the correct path at server level instead of reverse engineering in javascript in the browser. It may cause issues if you rearrange your project and asp changes it.