共用方式為


SP.SOD Class

Applies to: SharePoint Foundation 2010

Represents an on-demand ECMA script.

SP.SOD

Example

The following example registers a link to an ECMAScript(Jscript, JavaScript) file and creates an input button on an application page that calls a function defined in the script file. The function displays a simple "Hello" message on the page.

First, create a file named "MyScript.js" in the "\Program Files\Common Files\Microsoft Shared\Web server extensions\14\Template\Layouts\1033" folder, and add the following code to the file:

function sayHello() {
  alert("Hello");
}
//Notify SharePoint that the custom file has loaded.
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("myscripts.js");

Then add the following code to the application page.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    function runCode() {
        var x = ExecuteOrDelayUntilScriptLoaded(sayHello, "MyScript.js");
    }

</script>

    <SharePoint:ScriptLink ID="ScriptLink1" runat="server" language="javascript" Name="MyScript.js"/>
    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

See Also

Reference

SP.SOD Methods

SP Namespace