I've successfully implemented examples working with button or link click event handler. But when I try to use the code with
onload I get an error.
Assuming I've my
ViewItem() function defined in the head.
If I use this code in the body:
all is fine.
If I try with classic
onlaod, the call fails:
The line of code in
ViewItem function which generates the error is as follows:
var myContext = new SP.ClientContext.get_current();
This code works fine with the onclick event handler, but fails with the onload event handler with error:
SP.ClientContext is null or not an object
How can I solve this issue? Why it happens only when the function is called with
I've tried managing the
ExecuteOrDelayUntilScriptLoaded as suggested by the answers below, but no success. The behavior is as follows now:
The onclick works fine.
As soon as I add the code
ExecuteOrDelayUntilScriptLoaded in the HTML body, the ViewItem function does not get executed and the onclick returns the error message which I've described in my initial question.
If I remove the
onclick and leave the
onload only, no error is returned but the ViewItem does not get executed.
I've found that it works fine with jQuery only. Don't know why.
Code Details: https://gist.github.com/1744459