为什么我的 .ascx 文件 (Sharepoint 2010) 无法识别 indexOf?

Why is indexOf not recognized in my .ascx file (Sharepoint 2010)?

我的 Sharepoint 应用程序中有此代码:

function upsertPostTravelListItemTravelerInfo1() {
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');        
    this.website = clientContext.get_web();
    currentUser = website.get_currentUser();
    var commonEffluvia = 'i:0#.f|smp|';
    if (currentUser.indexOf(commonEffluvia) > -1) {
        currentUser = currentUser.substr(commonEffluvia.length, currentUser.length - commonEffluvia.length);
    }
    . . .

减少 currentUser 是必要的,因为出于某种原因它经常 returns 'i:0#.f|smp|' + 用户的电子邮件地址,例如 ''i:0#.f|smp|clayshan@ucsc.edu' 而不是简单地 'clayshan@ucsc.edu'

这段代码在 jsfiddle 中工作得很好,正如我的 fiddle faddle here 所证明的那样,但在上面的代码中,运行 在 Chrome, 我明白了,'Uncaught TypeError: currentUser.indexOf is not a function'

为什么 jsfiddle 识别的 Javascript 方法无法被 Chrome 或 Sharepoint(或 "whoever" 问题所在)识别?

简短的回答是 currentUser 将是 SPUser 类型的对象,而不是字符串。

通过调用 clientContext.load(currentUser) 然后调用 clientContext.executeQueryAsync() 来混合该变量后,您将能够访问其成员方法,例如 currentUser.get_loginName()currentUser.get_title()(微软的 JavaScript class 库文档中似乎都没有完整记录),这将允许您直接访问特定的用户属性......这应该比尝试更容易解析出那股恶臭。