Javascript running on Page tab

Anthony Levine asked on November 22, 2016 17:55

Is there any reason Javascript (added using the Javascript control) runs on the Page tab? It's preventing me from editing text in an editable text box. I now have to check whether the URL contains "/admin" before running to prevent this. To test this, add a Javascript control in the design mode on any page, add the statement alert('Test'); into the "In-line script" box. You can include the <script> tags or select to have them added and Save. Then go to the Page tab and you'll be met with an alert box. Is there a practical reason to allow Javascript to run on the Page tab?

Recent Answers


Peter Mogilnitski answered on November 22, 2016 18:09

Well if you want to prevent pop up in the edit mode you can do it with macro: For your javascript web part properties Behavior/Enabled you can put something like {%PortalContext.ViewMode == "LiveSite"@%} - this means your javascript alert will work only on the live site. When you edit your document you will not see the alert box.

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on November 22, 2016 18:13 (last edited on December 10, 2019 02:30)

That's correct. As Peter mentioned, you can prevent any code to be executed only on the live site. The whole macro could look like this:

{% if (PortalContext.ViewMode == "LiveSite") { %}
HTML code including SCRIPT tags executed only on the live site
{% } |(identity)GlobalAdministrator%}
2 votesVote for this answer Mark as a Correct answer

Anthony Levine answered on November 22, 2016 19:45

I was under the impression that using the Javascript control would prevent Javascript from running within the admin panel on the Page tab. Thank you both for your answers.

0 votesVote for this answer Mark as a Correct answer

Melissa King answered on June 19, 2018 02:31

The Page tab appears to have its own body class .EditMode, so I simply changed my selector in JS to body:not(.EditMode). This works on the Page tab but sadly not on the Design tab, but suits my purposes.

1 votesVote for this answer Mark as a Correct answer

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