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?

SandeshR
  • 1,992
  • 17
  • 67
  • 115
user9167
  • 335
  • 1
  • 4
  • 12

2 Answers2

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.

JC Vivian
  • 386
  • 2
  • 4
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");