Sharepoint - How do I know if the page is in Edit Mode from JavaScript?
Looks like you're looking for MSOLayout_InDesignMode
var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
if (inDesignMode == "1")
{
// page is in edit mode
}
else
{
// page is in browse mode
}
This will refer to value of the following html input control, which is rendering on the page when it is in edit mode:
<input type="hidden" name="MSOLayout_InDesignMode" id="MSOLayout_InDesignMode" value="1" />
Update: for wiki pages, you will need _wikiPageMode parameter:
var wikiInEditMode = document.forms[MSOWebPartPageFormName]._wikiPageMode.value;
if (wikiInEditMode == "Edit")
{
// wiki page is in edit mode
}
else
{
// wiki page is not in edit mode
}
SP.Ribbon.PageState.Handlers.isInEditMode()
Returns true or false. I use this in publishing pages.
There is also a global object available called 'PageState
'. Among the various members of state information is "ViewModeIsEdit
". If the value of this member is "1" then you are in edit mode (e.g. PageState.ViewModeIsEdit === "1"
).
Note: This variable is initialized after sp.ribbon.js gets loaded and is initialized from the _spBodyOnLoadFunctionNames
array on body load. So if you plan on using this and need to query a value as soon as the page loads, you will need to do a check to see if the object exists yet.