2

I'm follwing this tutorial to branding master page and totally successful. http://sharepoint.rackspace.com/sharepoint-2013-how-to-change-sharepoint-branding-text-in-the-upper-left-part-of-your-screen But when Im trying to do the same step with custom.js . I've got a blank page. Can you guys help me :). Here's my link to add script :

<SharePoint:ScriptLink language="javascript" name="custom.js" OnDemand="true" runat="server" Localizable="false" />

I've added it right under

<SharePoint:CssRegistration ID="CssRegistration1" name="&lt;% $SPUrl:~sitecollection/style library/custom.css %&gt;" runat="server" after="style.css" />

3 Answers 3

2

SharePoint:ScriptLink requires you to use SP.SOP.execute in order to load the script. Like this (reference):

SP.SOD.executeOrDelayUntilScriptLoaded(myfunction, 'sp.js');
function myfunction() {
  // my custom code
  //
  SP.SOD.executeOrDelayUntilScriptLoaded(myfunction2, 'custom.js'); //change
}

SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("sp.js");

Try using the <script> tag instead to have it always load.

<script type="text/javascript" src="~site/style library/js/custom.js" runat="server" />

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.scriptlink.ASPX

8
  • After add <script type="text/javascript" src="~site/style library/js/custom.js" runat="server" /> I got Error "Sorry, somethings went wrong". Even I've try to changed ~sitecol ~sitecollection instead ~site . Because I'm working in sitecollection Commented Sep 11, 2014 at 3:46
  • ahhh, I've fixed . there's no runat="server" in script tag. My site loaded, but went i'm press F12 . in Console I've seen 3 errors . Uncaught TypeError: Cannot read property 'onsubmit' of null ScriptResource.axd? Uncaught ReferenceError: theForm is not defined (index):784 Uncaught ReferenceError: theForm is not defined Commented Sep 11, 2014 at 4:07
  • did you have the function above in your code?
    – Mike
    Commented Sep 11, 2014 at 4:41
  • in .master I've added : "<script type="text/javascript" src="../../Style Library/Js/custom.js"/>" . In my custom.js "SP.SOD.executeOrDelayUntilScriptLoaded(myfunction, 'sp.js'); function myfunction() { var x= "hello"; alert(x); SP.SOD.executeOrDelayUntilScriptLoaded(myfunction2, 'custom.js'); } SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("sp.js");" Commented Sep 11, 2014 at 6:08
  • THen I have one more error "Uncaught ReferenceError: myfunction2 is not defined " Commented Sep 11, 2014 at 6:09
1

What is it you're trying to achieve here and why does that tutorial make it so unnecessarily complicated??

If you simply want to add some custom JavaScript or jQuery-enable your site, then simply reference them in the masterpage using tags as already stated.

Then in your custom.js add any functions you want to occur after page load into $(document).ready() function. And define any on demand functions and references outside this.

I stronlgy advise leaving any SOD methods well out of a global js file until you have managed to master a simple alert.

Clear the contents of your custom.js and add the following as a test:

jQuery(document).ready(function()
{
    alert('My custom.js loaded);
});

Once you have that working....slowly add additional functionality.

3
  • it alert 'My custom.js loaded' . but it effect to sharepoint site, when i'm F12 in browser and I saw 3 errors in console window. Commented Sep 12, 2014 at 1:59
  • Uncaught TypeError: Cannot read property 'onsubmit' of null ScriptResource.axd? ------ Uncaught ReferenceError: theForm is not defined (index):784 ----- Uncaught ReferenceError: theForm is not defined Commented Sep 12, 2014 at 2:00
  • @user3622351 these are unrelated to your custom javascript file and are coming from other scripts. There can often be dozens loaded for each SharePoint page. Why are you focusing on warnings and errors from sources you can't control?
    – Panoone
    Commented Sep 15, 2014 at 0:51
1

For a Custom User Action (CUA) you need to reference using a token, I was messing around with using an anonymous public CDN to host my scriptsrc. Couple of odd behaviors, when referenced with a full path it failed with 2 exceptions, most notably when i added a folder path /_layouts/15/xxx..js the CUA started working.

Post about it here: http://blog.sharepointsite.co.uk/2017/11/using-cdn-for-custom-user-actions.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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