11/24/2017

SharePoint: Running JavaScript Code Only When in Page Edit Mode


One of my old Content Editor Web Part “tricks” would not work when run in a Publishing page. I was hiding a web part when not in Page Edit mode. You can detect this mode by checking for ‘PageState.ViewModeIsEdit != "1"’. The PageState object is automatically created in normal site pages (“Wiki Pages”). It is also created in Publishing Pages, but only when the page has not been published. I.e. when the page is checked out, or in edit mode.

My code originally looked like this:

  if ( PageState.ViewModeIsEdit != "1" )
  {  …code to run when not in edit mode… }


As the PageState object does not exist in a Published page, I had to change it to this:

  if ( typeof PageState == 'undefined' || (PageState.ViewModeIsEdit != "1") )
  {  …code to run when not in edit mode… }


Of course… none of the above works in SharePoint Online “Modern UI” pages!


.

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.