16

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?

1
  • Why don't you like to have your code as code formatted? In my option this makes the question clear. Commented Jul 5, 2012 at 6:42

2 Answers 2

22

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.

2
  • Fixed Line breaks between links. Commented Jun 28, 2012 at 11:22
  • @Vardhaman I still want to find a good MDN link for anonymous functions as its is more on topic Commented Jun 28, 2012 at 15:43
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.