Script Editor only runs JavaScript in edit mode




If you need to embed script into a content editable page in SharePoint 2013/Online, you may decide to use the new Script Editor web part. There are often many preferable ways to add script to a page (e.g. via the master page, a custom action, custom control, the ScriptLink property, etc.) however this is an easy option for demo purposes or when deployment activities are out of scope.

There is a gotcha for those who like to skip attributes that may have seems verbose in the past. Any JavaScript which you include via the Script Editor web part must be wrapped in the <script> tag otherwise it will be rendered as text. However, if you fail to provide the required type='text/javascript' or language='javascript' attribute to the script tag then the code will continue to run when the page is in edit mode but will fail to execute when the page is saved and then viewed.

Note the attribute, you need it!
Note the language attribute, you need it!

CAVEAT: As I stated in the first paragraph, this is often not the best way to add script to a page.




4 thoughts on “Script Editor only runs JavaScript in edit mode”

    1. I find that script editor scripts run when the page first enters ‘edit mode’ but after subsequent page post-backs while the page remains in this mode (e.g. edit web part properties) the scripts don’t run. I think that this should be considered preferable behaviour as it makes it possible to remove script editors which might otherwise be breaking the page.
      When you say ‘turn on a filter’, I’m not sure exactly what you are referring to but I assume that this causes a post-back as discussed above.

  1. Indeed don’t forget to add type=”text/javascript” else your script will only run within the edit mode of the page and not within displaymode. This issue only occurs while the “Minimal Dowload Strategy” site feature is activated.

Leave a Reply

Your email address will not be published.