Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

Thanks!

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
add comment

1 Answer 1

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. –  Pathachiever11 Jun 6 '12 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 –  Wictor Wilen MCA MCM MVP Jun 6 '12 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! –  Pathachiever11 Jun 6 '12 at 17:16
add comment

Your Answer

 
discard

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.