Detect if page is on experience editor Sitecore 9 via Javascript?












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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.







      experience-editor presentation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 10 hours ago









      Patrick Barron

      1,6261720




      1,6261720










      asked 10 hours ago









      jsPlayerjsPlayer

      1134




      1134






















          4 Answers
          4






          active

          oldest

          votes


















          3














          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/






          share|improve this answer
























          • 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
            9 hours ago



















          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)">





          share|improve this answer

































            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>





            share|improve this answer

































              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()

              }
              }





              share|improve this answer

























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "664"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f17230%2fdetect-if-page-is-on-experience-editor-sitecore-9-via-javascript%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                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/






                share|improve this answer
























                • 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
                  9 hours ago
















                3














                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/






                share|improve this answer
























                • 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
                  9 hours ago














                3












                3








                3







                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/






                share|improve this answer













                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/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 10 hours ago









                Vlad IobagiuVlad Iobagiu

                13.3k21034




                13.3k21034













                • 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
                  9 hours ago



















                • 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
                  9 hours ago

















                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
                9 hours ago





                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
                9 hours ago











                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)">





                share|improve this answer






























                  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)">





                  share|improve this answer




























                    2












                    2








                    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)">





                    share|improve this answer















                    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)">






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 8 hours ago

























                    answered 9 hours ago









                    Dan SinclairDan Sinclair

                    2,066626




                    2,066626























                        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>





                        share|improve this answer






























                          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>





                          share|improve this answer




























                            1












                            1








                            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>





                            share|improve this answer















                            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>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 8 hours ago

























                            answered 9 hours ago









                            x3mxrayx3mxray

                            1714




                            1714























                                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()

                                }
                                }





                                share|improve this answer






























                                  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()

                                  }
                                  }





                                  share|improve this answer




























                                    0












                                    0








                                    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()

                                    }
                                    }





                                    share|improve this answer















                                    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()

                                    }
                                    }






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 9 hours ago

























                                    answered 9 hours ago









                                    jsPlayerjsPlayer

                                    1134




                                    1134






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Sitecore Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f17230%2fdetect-if-page-is-on-experience-editor-sitecore-9-via-javascript%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                        Alcedinidae

                                        Can an atomic nucleus contain both particles and antiparticles? [duplicate]