2013-05-09 103 views
1

我已經研究過這個非常徹底,每個人都說我下面的代碼應該加載SP.js,但我無法加載它。SP.js爲什麼不加載?

調試,我得到:

NewForm.aspx, line 1667 character 5 
SCRIPT5009: 'PeoplePicker' is undefined 

,並沒有看到下圖來源SP.JS。

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" 
    Localizable="false" /> 
<script type="text/javascript"> 

ExecuteOrDelayUntilScriptLoaded(SetWebUserData(), "SP.js"); 

function SetWebUserData() { 
    var pplPicker = new PeoplePicker(); 
    // Set the parent tag id of the people the picker. 
    pplPicker.SetParentTagId('Main_x0020_Contact'); 
    pplPicker.SetLoggedInUser(); 
    }; 
</script> 

非常感謝任何幫助。

+0

'SP.js'加載正常。該錯誤是說你還沒有定義類PeoplePicker。如果有,那麼該類或其定義的腳本無法正確加載時出錯。 – 2013-05-09 14:04:45

+0

我已經在SP.js文件中定義了PeoplePicker,甚至當我刪除所有的代碼並使PeoplePicker成爲警報函數時,我也沒有收到警報。我不相信sp.js加載正常。 – user2366475 2013-05-09 14:19:14

+0

如果我在源代碼中包含PeoplePicker,我仍然在這一行上得到一個錯誤 – user2366475 2013-05-09 14:26:28

回答

5

您正在使用ExecuteOrDelayUntilScriptLoaded錯誤。您應該只傳遞給函數的名稱,它應該是這樣的:

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "sp.js"); 

沒有()

1

第一個參數Exec​​uteOrDelayUntilScriptLoaded必須是一個功能。 在請求的腳本文件被載入後調用該函數。

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" 
    Localizable="false" /> 
<script type="text/javascript"> 

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "SP.js"); 

function SetWebUserData() { 
    var pplPicker = new PeoplePicker(); 
    // Set the parent tag id of the people the picker. 
    pplPicker.SetParentTagId('Main_x0020_Contact'); 
    pplPicker.SetLoggedInUser(); 
    }; 
</script> 

有(),您通話功能。 這意味着,你的錯誤是通過你的函數的結果作爲參數,而不是函數本身。

實例更好地理解:

function helloFunction() { 
    return 42; 
} 

var myHelloFunction = helloFunction; // Function is passed 
var myHelloFunctionResult = helloFunction(); // Result of your function (42) is passed 
0

我知道這是有點晚了,但我認爲這是你正在尋找的答案。

SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function(){ 
    var pplPicker = new PeoplePicker(); 
    // Set the parent tag id of the people the picker. 
    pplPicker.SetParentTagId('Main_x0020_Contact'); 
    pplPicker.SetLoggedInUser(); 
};