Thursday, July 22, 2010

ResolveUrl in Javascript

ResolveUrl in Javascript


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.

No comments:

Post a Comment

Popular Posts