0

In my custom masterpage i have this

<SharePoint:ScriptLink runat="server" name="~SiteCollection/Style Library/js/jquery-1.12.4.min.js" language="javascript" /> 
<SharePoint:ScriptLink runat="server" name="~SiteCollection/Style Library/js/BindMENU.js" language="javascript" /> 

and few lines below that i have this

<script type="text/javascript">
  jQuery(function($) {
    $('#MenuH .MenuBar ul.root').BindMENU({ MenuStyle:"DefaultMenu",Orientation:"Horizontal" });
  });
</script>

In case you are wondering i am using a jquery function BindMenu which I is defined within the BindMENU.js file which i loaded earlier via SharePoint:ScriptLink

On loading the page I get this error -

error.png

BUT If i put the call to BindMenu.js in a document.write like this

<script type="text/javascript">
   document.write("<script type='text/javascript' src='"+path+"js/BindMENU.js'><" + "/script>");
</script>

THEN the error goes away

I am not sure why this is occurring.

What is the difference using SharePoint:Scriptlink and using document.write to reference a Jquery/JavaScript function?

Can anybody here offer any insight ?

2

First - never use document.write in sharepoint. It breaks MDS and is bad practise (Reference).

In your case it's just a workaround to load the plugin later in the page lifycycle, but actually your main problem is, that the plugin is either not loaded at all, overwritten or you call it too early and it's not initialized when you try to call it.

Troubleshooting:

Is the plugin loaded / loaded at the right time in the lifecycle?

To check that you can easily check your browser dev tools --> your scriptblock called before you referenced the plugin? If not move your references.

Is the plugin overwritten?

If there are multiple jQuery references, the plugin is possibly overwritten by a later added jQuery reference.

Example:

Added jQuery -> Added Plugin -> Added jQuery = jQuery without plugin

  • On some more investigation, I noticed that on a few pages the call to BINDMenu function using SharePoint:ScriptLink seems to be working fine but not on others where I had to resort to document.write. I think it has probably something to do with what you indicated. – vivek m Sep 23 '16 at 15:54
2

When you use SharePoint:ScriptLink. you should be a ware of

  • Ensures that the script is loaded only once;
  • Ensures that other dependencies have been loaded first;
  • By default looks to LAYOUTS/%LanguageID% folder;
  • Can be used with ~site and ~sitecollection tokens.

In your case, you can use the SharePoint:ScriptLink as the following

<SharePoint:ScriptLink runat="server" ID="sl" Localizable="False" LoadAfterUI="False" Name="myscript" ></SharePoint:ScriptLink>

or You can also use this tag script directly without document.write()

<script type='text/javascript' src='../js/BindMENU.js'></script>

For more details check ADDING CSS AND JAVASCRIPT FILES TO SHAREPOINT 2013 MASTERPAGE

  • "The ScriptLink will always look in the /_LAYOUTS/1033 for the scripts" - really ? If you notice I have the JQuery library call also in a ScriptLink and that is lot located at /_LAYOUTS/1033 either. But the JQuery seems to load OK. Will do more digging on Scriptlink. – vivek m Sep 23 '16 at 15:00
  • 1
    That's not entirely true. My guess is that if you only specify a file name in the name attribute it will look for it in the /_layouts/1033 folder. But I can confirm that if you use a URL token like ~sitecollection and a path to the script file, a ScriptLink will load the script from just about anywhere (i.e. somewhere else in the _layouts folder, or from somewhere on a site like Site Assets or the Style Library). – Dylan Cristy Sep 23 '16 at 15:02
  • I agree with all, I am sorry this what was mentioned at reference , sharepoint.stackexchange.com/questions/114323/… thomasdaly.net/2010/12/16/scriptlink-vs-whats-the-difference ,anyway I will update my answer, and thanks for your hint – Mohamed El-Qassas MVP Sep 23 '16 at 15:16

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.