detect viewmode in javascript?

kyle shapiro asked on June 8, 2015 18:04

I would like to detect if the site is in live mode using JavaScript. I have some text that is set hidden or visible by JavaScript. I want to have this JavaScript only run when the site is in live-mode. This would save my content managers some grief. Thank you.

Correct Answer

Brenden Kehren answered on June 9, 2015 13:28

I apoligize, wrong syntax, I was thinking C# not a macro. Try {% ViewMode == "LiveSite" %}

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 8, 2015 18:07

Use the JavaScript webpart, this does it automatically.

2 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on June 8, 2015 18:39

Hi Brenden, thanks for the reply. My JavaScript webparts run while in edit mode, page tab. Is that not the default behavior? I want to prevent my JavaScript webparts from executing when CMs edit through the edit mode, page tab.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 8, 2015 19:32 (last edited on December 10, 2019 02:30)

When I checked the source of that webpart it does check to see if live site or not. You can place a macro in the visibility/enabled property of the macro as well just to be sure.

{% ViewMode.LiveSite |(identity)GlobalAdministrator%}

That should do the trick.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on June 8, 2015 20:30 (last edited on December 10, 2019 02:30)

Do I need to write a statement saying "if {% ViewMode.LiveSite |(identity)GlobalAdministrator%} into the enabled property of a js webpart, but it still fires on the edit-page page. I'm using a test js webpart that just says "alert('hi');", inline.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on June 9, 2015 17:17

That worked! Thank you Brenden, I will definitely use this. Where could I find reference material for the other viewmode macro parameters?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 9, 2015 20:45

You can find more ViewMode information in the API documentation.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on June 10, 2015 20:01

I see the members for the ViewModeEnum Enumeration, but how did you know it was ViewMode.LiveSite and not ViewModeEnum.LiveSite ? I am unable to find in the API where it says ViewMode alone. Is it an implied naming convention that the various enumerations will be written without the Enum suffix?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.