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" });

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 -


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>");

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 Answers 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.


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.


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, 2016 at 15:54

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>


  • "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, 2016 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). Sep 23, 2016 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 Sep 23, 2016 at 15:16

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.