This is something that is super easy, yet I get asked about it quite often.
Here’s how you do it:
In the master page for the site, put this:
<script type="text/javascript">
var baseUrl = "<%= ResolveUrl("~/") %>";
</script>
Then, in your javascript file, put this function:
function ResolveUrl(url) {
if (url.indexOf("~/") == 0) {
url = baseUrl + url.substring(2);
}
return url;
}
You could have put the function right in the master page, but then you wouldn’t get intelli-sense on it for the rest of your code.
Now you can call ResolveUrl with ~/ right from javascript.
Super easy, but also super useful!
If you use themes, you might even want to write something that does a “get themed url” where the current theme is output from the master page via Page.Theme.
more later – joel.