1

I have a js script and I want to apply it on a whole SP site. I inserted a content editor webpart on the start page and it works fine, but now i want to apply it on the whole site. Do I have to go through every page and insert a content editor webpart with the lines I need, or is there way to put it on one place and apply it on the whole site?

script.js:

$(function () {
getUser();
});

function getUser() {
    var clientContext = new SP.ClientContext();
    var web = clientContext.get_web();
    var user = web.get_currentUser();
    clientContext.load(user);
    clientContext.executeQueryAsync(function(){
        var leftNav = document.getElementById("DeltaTopNavigation");
        leftNav.innerHTML += "<p>User:" + user.get_title() + "</p>";
        //document.write("<p>User:" + user.get_title() + "</p>");
    }, function(){alert(":(");});

}

and i calling it with this code in the content editor/seattle.master:

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/{PATH}/jquery-latest.min[1].js"></script>
<script type="text/javascript" src="/{PATH}/script.js"></script>
3

If you want to apply script to whole site, than you have to modify your Masterpage. You cant apply scripts to whole site using content editor unless you manually place content editor webparts on each and every page in the site.

To achieve what you want, just follow this simple steps

Just go to sharePoint designer-->make a copy of default Masterpage--> set custom Masterpage as default Masterpage-->place your script in Masterpage!

  • in my case, this would be seattle.master page...and I tried it but it doesn't work that well (sometimes it loads the script, sometimes it loads and then when the whole page is loaded it disapears, and sometimes it works) and not on all pages :S – Luka Klarić Sep 7 '16 at 8:11
  • you store your script in some library and refer it from there. if script has some errors, sharepoint automatically deletes it. so better save it somewhere and refer it using a link – SharePointMan Sep 7 '16 at 8:16
  • Can you post the relevant code, Luka? – grisha Sep 7 '16 at 8:16
  • I edit the post and added the script. there is error. like i said, if i call the script in the content editor of the start page it works fine every time i reload. but when i add it in the head of seattle.master it works like in the comment above :( – Luka Klarić Sep 7 '16 at 8:39
  • 1
    Probably you need to delay loading of the script when the page is ready. Have a look at this thread it should point you in right direction: stackoverflow.com/questions/16936250/… – Marek Sarad Sep 7 '16 at 10:28

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.