4

Ok, i see many examples of this online but none seems to work, maybe those examples are for older Sitecore. I simply want to detect if the user is on regular site view or if they are on experience editor using some JS. I was messing with console and saw this method Sitecore.PageModes.PageEditor.ExperienceEditor.length but it always return 0. I saw many examples with c#/razor but interested in js for now.

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

if(isPageEditor()) {
    // Write your logic here
}

Here are some more details:

https://code.askmein.com/how-to-detect-page-editor-mode-in-sitecore-through-code/

https://mattneil.co.uk/2016/12/08/detect-sitecore-page-modes/

  • i actually tried this but on regular view i am getting this error "Uncaught ReferenceError: Sitecore is not defined at pageEditorMode"" it works on editor mode . Basically i am trying to disable sticky nav on experiance editor. i put a try catch around like below to solve for now. Is there a better way to solve this? . i am putting code as one of the answer – jsPlayer Mar 5 at 20:07
2

This has worked for us. Note that we adjusted our layout view to add the sc-edit-mode CSS class to the body, which also helps identify Experience Editor mode.

isPageEditor() function

function isPageEditor() {
    return (!!((typeof Sitecore !== "undefined") && (typeof Sitecore.PageModes !== "undefined") && (typeof Sitecore.PageModes.PageEditor !== "undefined")) || (document.body && document.body.getAttribute("class") === "sc-edit-mode"));
};

View modifications

<html class="@(Sitecore.Context.PageMode.IsExperienceEditor ? "sc-edit-mode" : string.Empty)">
1

Sitecore.PageModes.PageEditor.ExperienceEditor is an internal EE function that you can`t call direct to return true/false.

As @Vlad mentioned, in EE you can check it with

var isExperienceEditor = !!(Sitecore && Sitecore.PageModes && Sitecore.PageModes.PageEditor)

(it means that all these objects are not undefined == you are in EE).

But 'Sitecore' namespace doesn`t exist in Normal page mode and as a workaround you can use your custom global variable that is initialized in a layout of your page (with razor or aspx syntax)

<script>
    var isExperienceEditor = Json.Encode(Sitecore.Context.PageMode.IsExperienceEditor);
</script>
0

Thanks to vlad lobagiu. I tried vlad lobagiu answer but on regular view i am getting this error "Uncaught ReferenceError: Sitecore is not defined at pageEditorMode"" it works on editor mode, i am guessing sitecore object is not available on published site(make sense) . Basically i am trying to disable sticky nav on experiance editor, I put a try catch around like below to solve for now. Is there a better way to solve this?

    try {
     // Sitecore global name space in page editor or preview mode
     var pageEditorMode = function () {
        return !!(Sitecore && Sitecore.PageModes && Sitecore.PageModes.PageEditor);
     };

     if (pageEditorMode()) {
        console.log("Experience editor detected, not adding sticky white header")
     }
     else {
        function()

     }
  } catch (error) {
     if (error) {
                    function()

     }
  }
0

Here's what I'm using which works in regular view too (and no double bang (!!) or try/catch needed):

const isExperienceEditor = () => window.Sitecore && Sitecore.PageModes && Sitecore.PageModes.PageEditor ? true : false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.