Monday, April 27, 2009

JavaScript Funtions to get Query String Value, Host Name and Site Url

function GetQueryString( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
{
if(results[1].indexOf('+')!= '-1')
{
results = results[1].replace('+',' ');
return results;
}
if(results[1].indexOf('%')!= '-1')
{
results = results[1].replace('%20',' ');
return results;
}
else
return results[1];

}
}


function getHost()
{
var url = window.location;
var urlparts = url.split('/');
var host = urlparts[0];
return host;
}

function GetSiteUrl()
{
var siteUrl = window.location.href;
var copyOfSiteUrl = siteUrl;
siteUrl = siteUrl.split("/");
var isSubSite = false;
var url = null;

if(siteUrl[3] == 'sites')
{
isSubSite = true;

}
if(isSubSite)
{
copyOfSiteUrl = copyOfSiteUrl.split("sites/");
var subSiteName = copyOfSiteUrl[1].split("/");
url = copyOfSiteUrl[0] + "sites/" + subSiteName[0];
return url;
}
else
{
copyOfSiteUrl = copyOfSiteUrl.split("/");
url = copyOfSiteUrl[0] + "//" + copyOfSiteUrl[2];
return url;
}
}

No comments: