SharePoint - How To Check If Page Is In Edit Mode Or Not In JavaScript

As SharePoint has evolved and other client technologies have started to become popular and independent, we are achieving most of the solutions with client side technologies like JavaScript, jQuery, and other similar kinds.

I came across one requirement while working on a JavaScript based component where I need a conditional code to run only when my page is in Edit mode. I found multiple solutions which I have summarized below.

Option 1

  1. var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;  
  2. if (inDesignMode == "1")  
  3. {  
  4.    // page is in edit mode  
  5.    }  
  6.    else  
  7.    {  
  8.       // page is in browse mode  
  9.    }  

For Wiki pages

  1. var wikiInEditMode = document.forms[MSOWebPartPageFormName]._wikiPageMode.value;  
  2. if (wikiInEditMode == "Edit")  
  3. {  
  4.    // wiki page is in edit mode  
  5. }  
  6. else  
  7. {  
  8.    // wiki page is not in edit mode  
  9. }  

Option 2

Using SP.Ribbon.PageState.Handlers.isInEditMode()

  1. ExecuteOrDelayUntilScriptLoaded(function(){  
  2. var InEditMode = SP.Ribbon.PageState.Handlers.isInEditMode();  
  3. if(InEditMode){  
  4.    // page is in edit mode  
  5.    }  
  6. }, 'SP.Ribbon.js');  

Option 3

While checking the value in SharePoint search display template, we need to use Srch.U.isPageInEditMode Method

  1. var isRollupPageInDisplayMode = Srch.ContentBySearch.isRollupPage(ctx.ClientControl) && !Srch.U.isPageInEditMode();  

Happy Learning!