Insights

Detecting Sitecore PageEditor Mode In JavaScript

In Case Of Emergency

Depending on how you write your JavaScript, conflicts can arise between Sitecore's native JavaScript and your own. Particularly in PageEditor mode.

Our solution for this is to use RequireJS so all of our JavaScript is modular and scoped to not interfere. We have some old posts on RequireJS and Sitecore, but that's a bigger discussion.

Right now, I just want to share quick JavaScript snippet for detecting PageEditor mode. Use it as you like:

Detect PageEditor Mode


var isPageEditor = function(){
    return !!(Sitecore && Sitecore.PageModes && Sitecore.PageModes.PageEditor);
};

if(isPageEditor()) {
    // I love PageEditor
}

Take advantage of sequential ANDing and use the double-bang to convert Sitecore.PageModes.PageEditor into a true or false boolean value.

I'm sure this is one of many ways to detect PageEditor mode. I have a soft spot for JavaScript one-liners so I thought I'd share.

The article was authored using Markdown for Sitecore.

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet Dan Cruickshank

President | Sitecore MVP x 11

Dan is the founder of Fishtank. He's a multi-time Sitecore MVP and Coveo MVP award winner. Outside of technology, he is widely considered to be a top 3 father (routinely receiving "Father of the Year" accolades from his family) and past his prime on the basketball court.

Connect with Dan