Convert Relative Url to Absolute Url in SharePoint using Javascript

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 FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS1033

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

Leave a Reply

Your email address will not be published. Required fields are marked *