I have a giant script block inside my code file and I want to load it after SP.js loads. I've looked at ScriptLink, but I don't think ScriptLink injects code, it only injects code files.

Is there a better way to accomplish this?


PS: In case you're wondering why I have the JS file in my code: its because I have to update the JS file depending on some settings.

1 Answer 1


You could use the ExecuteOrDelayUntilScriptLoaded function and inject your Javascript like this:

ExecuteOrDelayUntilScriptLoaded(function() {
    //insert your JS stuff here
}, 'sp.js'); 

This way the code in the anonymous function will be executed when sp.js is loaded.

  • I implemented your suggestion, but I get this error: unterminated string literal. I have the following code inside the function -> $('head').append("big, neat javascript");. I have double checked everything and the script is perfect, I'm guessing it might be related to javascript? The script is huge.
    – harsimranb
    Jun 6, 2012 at 0:41
  • 1
    You got something wrong with your JavaScript most likely. And, please consider a redesign; set a number of variables in the dynamically injected JS and use a normal JS file with the logic Jun 6, 2012 at 9:30
  • 1
    Thanks for the advice, I totally did a redesign of my plan and came up with a better design. I just created a global variable from code behind, and put unique values in it so I can do my craziness. Thanks for that advice!
    – harsimranb
    Jun 6, 2012 at 17: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.