I have a page on my sharepoint site, when i'm on the edit page the javascript works however when I have published the page, the javascript doesn't work. I have added the link to the javascript file in some embedded code. any help would be appreciated
3 Answers
It's a known issue for a publishing pages (here or here), sp.js
library is loaded in the edit mode but not in the display mode.
As part of Minimal Download Strategy (MDS) that was introduced in In SharePoint 2013, SharePoint JavaScript libraries like sp.js
are not loaded until explicitly called.
Solution
Utilize SP.SOD.executeFunc method to load SharePoint JavaScript libraries on demand, for example:
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
//your code goes here...
});
-
Hello Vadim where do I put the SP.SOD.executeFunc in my code? Does it go in my sp.js file or my page somewhere? Commented Aug 17, 2017 at 17:14
-
That's right, sp.js is a part of core JS library and it is getting loaded with every page in SharePoint Commented Aug 17, 2017 at 18:26
You need to apply the type or language attribute to any <script>
tags being used in a Script editor web part.
type='text/javascript'
or language='javascript'
For example: <script type='text/javascript' src="js/app.js"></script>
Is your javascript placed inside PublishingWebControls:EditModePanel?
If yes, check if the pagedisplaymode is correct!
If you want the javascript to be run in both (edit/display) just place the javascript outside the PublishingWebControls:EditModePanel
<!-- Display mode only -->
<PublishingWebControls:EditModePanel runat="server"
PageDisplayMode="Display">
javascript....
</PublishingWebControls:EditModePanel>
<!-- Edit mode only -->
<PublishingWebControls:EditModePanel runat="server"
PageDisplayMode="Edit">
javascript
</PublishingWebControls:EditModePanel>
documentation to editmodepanel