Thursday, March 20, 2014

Obtain the URL of your root website using Javascript

If you ever need to obtain the URL to your root website via Javascript code (i.e. http://rootWebsite), I've got a couple of good methods for doing so which are presented as follows:

If you're on a SharePoint site want to use CSOM:

var clientContext = new SP.ClientContext();
var rootWebsiteUrl = clientContext.get_site.get_rootWeb();


If you're on a SharePoint site and don't want to use CSOM:

var rootWebsiteUrl = _spPageContextInfo.siteServerRelativeUrl;


If you're not on a SharePoint site or, actually, have no idea what SharePoint is:

var currentLocation = document.location.toString();
var webApplicationNameIndex = currentLocation.indexOf('/', currentLocation.indexOf('://') + 3);
var rootWebsiteUrl = currentLocation.substring(0, webApplicationNameIndex);

Friday, March 14, 2014

Centering an image horizontally using CSS

If you ever need to horizontally center an image on your web page, here is a simple way to do that via CSS:
<style>

.image_Container {
    display: block;
    margin-left:auto;
    margin-right: auto;

    /* BONUS - Add a stylish border around your image */
    border: 3px solid #FFFFFF;
    border-radius: 6px 6px 6px 6px;
    box-shadow: 0 1px 7px rgba(0, 0, 0, 0.15);
    /* For IE 8 */
    border: 1px solid #EFEFEF;
    -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#CCCCCC')";

</style>
...
<img class='image_Container' src='pictureUrl' />
As a bonus, I also included some CSS that will place a "stylish" border around the image, but feel free to drop that section if you'd prefer not to have a border around it.