2

I have tried this:

<script type="text/javascript">
    function myfunc()
    {
        ExecuteOrDelayUntilScriptLoaded(GetSuppliers, "Directory.js");
    };
    _spBodyOnLoadFunctionNames.push("myfunc");
</script>

As suggest on another thread but this does not work. If I use a link and specify javascript:functionname(); then it works.

Anyone have any ideas?

  • Matt
4
  • looks like correct code. But what is the context? What page? Commented Jul 6, 2012 at 19:53
  • Just a standard Web Part Page Commented Jul 6, 2012 at 20:00
  • Even added the code as a script in a Content Editor Web part and it is still not firing Commented Jul 6, 2012 at 20:00
  • OK after debugging it seems there is a problem with my javascript, myContext = new SP.ClientContext.get_current() seems to be undefined yet when I click the link it works fine. Commented Jul 6, 2012 at 20:09

1 Answer 1

4

The issue you are facing is that the sp.js (which holds the functions for the JavaScript Client Object Model) is not getting loaded before you call your function. It works when you click the link because the file is loaded before you click the link. We need to ensure that sp.js is loaded before you call GetSuppliers.

For that, we need to create a script dependency of the Directory.js on the sp.js. The RegisterSodDep will ensure that sp.js will be loaded before Directory.js.

Try this code:

<script type="text/javascript">
    function myfunc()
    { 
        RegisterSodDep("Directory.js","sp.js");
        ExecuteOrDelayUntilScriptLoaded(GetSuppliers, "Directory.js");
    };
    _spBodyOnLoadFunctionNames.push("myfunc");
</script>

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.