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

share|improve this question
    
looks like correct code. But what is the context? What page? –  gandjustas Jul 6 '12 at 19:53
    
Just a standard Web Part Page –  Mattmoo Jul 6 '12 at 20:00
    
Even added the code as a script in a Content Editor Web part and it is still not firing –  Mattmoo Jul 6 '12 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. –  Mattmoo Jul 6 '12 at 20:09
add comment

1 Answer

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>
share|improve this answer
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.