Convert Relative Url to Absolute Url in SharePoint using Javascript

Posted by Shantha Kumar on 9th July 2009

If you are come around core.js file, you all can see many functions available on that file.Those fuctions are very usefull when we need that in customization, for instance

We have a relative url of the current sharepoint site, I need to convert that relative url to absolute url, there comes a simple method called makeAbsUrl

The core.js file can be found @

<Root Directory>:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033

Method syntax:

makeAbsUrl(relativeUrl)

//Code in core.js
function makeAbsUrl(strUrl)
{
if (strUrl.length > 0 && “/”==strUrl.substr(0, 1))
{
strUrl=window.location.protocol+”//”+window.location.host+strUrl;
}
return strUrl;
}

Example 1:

makeAbsUrl(“/Lists/AllItems.aspx”);

The above function retuns a value http://server/Lists/Allitems.aspx

Example 2:

If we have a subsite, then we can use makeAbsUrl as

makeAbsUrl(L_Menu_BaseUrl+“/Lists/AllItems.aspx”);

L_Menu_BaseUrl returns a absolute path of the site.

L_Menu_BaseUrl = /sites/subsite

Now we have return value as http://server/sites/subsite/Lists/AllItems.aspx

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment