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.

Meet Dan Cruickshank


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