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 written a javascript client-object model code and I am calling it through the below line code from content editor webpart:

ExecuteOrDelayUntilScriptLoaded(getWebSiteData(sys), "sp.js");// throwing error

But it is throwing javascript error in init.js and other javascript functions are not working. Can someone tell me how to pass parameters to javascript client-model function?

share|improve this question
    
Why don't you like to have your code as code formatted? In my option this makes the question clear. –  Shegit Brahm Jul 5 '12 at 6:42

2 Answers 2

up vote 17 down vote accepted

You can use an function as a delegate and then call your function with in.

ExecuteOrDelayUntilScriptLoaded(function () { alertThis("Hello World") }, "core.js");

function alertThis(value)
{
    alert(value);
} 

It is worth reading about closures, delegates and Anonymous functions in javascript to help understand the code.
SO - How does an anonymous function in javascript work
SO - How do javascript closures work
Mozzila Developer Network Closures

Any comments to help improve this answer would be helpful.

share|improve this answer
    
Fixed Line breaks between links. –  JC Vivian Jun 28 '12 at 11:22
    
thanks will try this way. –  user9167 Jun 28 '12 at 11:26
    
Thanks it worked... –  user9167 Jun 28 '12 at 11:54
    
+1 for giving MDN link! –  Vardhaman Deshpande Jun 28 '12 at 14:30
    
@Vardhaman I still want to find a good MDN link for anonymous functions as its is more on topic –  JC Vivian Jun 28 '12 at 15:43

The answer suggested by JC Vivian works nicely, but in my opinion there's a cleaner, more readable way. You can create a function that returns the function that does what you need:

function dataGetterFor (param) {
    return function () {
        getWebSiteData(param);
    };
}
ExecuteOrDelayUntilScriptLoaded(dataGetterFor(sys), "sp.js");

This causes dataGetterFor(sys) to be evaluated and ExecuteOrDelayUntilScriptLoaded to be called with an anonymous function. A shorter but less extensible way to achieve the same result is the one-liner

ExecuteOrDelayUntilScriptLoaded(function () { getWebSiteData(sys); }, "sp.js");
share|improve this answer

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.