SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer
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 '12 at 0:41
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 – Wictor Wilen MCA MCM MVP Jun 6 '12 at 9:30
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 '12 at 17:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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