I need to add some components to master page if the user browsing my site home page.

note: i can't use the page URL as identifier, because the site owners can change the default home page for there sites.


This should help you out. You might want to tidy up the code that checks the welcome page against the current URL - at the moment it's just doing an indexOf.

var context = SP.ClientContext.get_current();
var web = context.get_web();
var rootFolder;
var welcomePage;

function getWeb() {
   rootFolder = web.get_rootFolder(); 
   context.executeQueryAsync(getWebSuccess, getDataFail);

function getWebSuccess() {
   welcomePage = rootFolder.get_welcomePage();
   context.executeQueryAsync(getWelcomePageSuccess, getDataFail);

function getWelcomePageSuccess() {
   // This is clumsy, needs to be improved
   if (location.href.indexOf(welcomePage) === -1){
       console.log("Not the welcome page");
   } else {
       console.log("This is the welcome page!");

// This function is executed if the above calls fail
function getDataFail(sender, args) {
   console.log('Failed to get the web Details. Error:' + args.get_message());

$(document).ready(function() {
  • thank you @dave, this is fantastic. I'm wandering if there is way to do that without waiting callback result, is there any information inside the page can help us to figure out is this a home page for the current site
    – jobin
    Mar 20 '15 at 11:34
  • Unfortunately, I don't think so. The first place you'd look for stuff like this is the very handy _spPageContextInfo object (that SharePoint creates for every page) and it isn't present there. Mar 20 '15 at 11:58
  • One thing you could use is the REST API. The "/_api/web/rootfolder" request will return some XML for the current site containing the welcome page, but you'd still need to make this call in JS (either synchronously or asynchronously), but it would allow you to get the information in one request. Mar 20 '15 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.