2

I'm having a strange problem with Javascript and SharePoint Online.

I've created a page that contains a script editor webpart with some JavaScript code (calls a few REST services to show a list of available document libraries on that site). When my page is in EDIT mode the code is working like it should. In normal mode the code is only working when I enter the url like this: https://server/site/SitePages/Overzicht.aspx?tag=ICT

When you click on an item and then you go back the url becomes: https://server/site/_layouts/15/start.aspx#/SitePages/Overzicht.aspx?tag=ICT And then it no longer works. It seems that the code in the script editor webpart is not being executed.

Anyone have a solution for this problem or can explain me what is happening here?

Code in script editor:

<script src="https://server/site/SiteAssets/jquery-2.1.3.min.js"></script>
<script src="https://studenthowestserver/site/SiteAssets/Howest.SP.ListHelper.js"></script>

<div id="documentlibraries"></div>
3

This is happening because of Minimal Download Strategy, which is by default enabled on SP Online.

To make it work, you should use SP.SOD.executeFunc

https://msdn.microsoft.com/en-us/library/office/ff409592%28v=office.14%29.aspx

This ensures that the specified file that contains the specified function is loaded and then runs the specified callback function.

1

Not sure if it is exactly the same, but I had a similar problem where some JavaScript code in a script editor web part only worked when the page was in edit mode.

It started working normally when I added the language and type attributes to the script tags.

See this answer.

  • Had the exact same problem - found out I forgot to add type="text/javascript" in the script tag. After adding this it worked fine! – Thorvald Bøe Nov 22 '16 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.