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 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.