我是Java语言的新手,我正在设置一个多语言Sharepoint。我想在每个页面中添加一个代码,以检查用户电子邮件和URL中的语言(葡萄牙语或西班牙语),然后重定向(或不重定向至)正确的语言URL。
条件正常,但处于无限循环中。
我在哪里做错了?
<script src="/SiteAssets/jquery.SPServices-2013.02a.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var sitefrom = window.location.href
var language = sitefrom.search(/portuguese/i); /* IF language = -1, then site from = SPANISH, otherwise site from is PORTUGUESE */
var site_es_pt = sitefrom.replace(/spanish/gi, "portuguese");
var site_pt_es = sitefrom.replace(/portuguese/gi, "spanish");
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccess,
error : onError
});
function onSuccess(data, request){
var loginName = data.d.Title;
if(loginName == "*@br.XX.com" && language == -1) {alert(loginName);}
if (loginName !== "*@br.XX.com" && language !== -1){alert(sitefrom);}
}
function onError(error) {
alert("error");
}
</script>
答案 0 :(得分:0)
根据我的经验,以上脚本不应导致无限循环,您如何使用脚本?尝试通过将脚本编辑器Webpart插入页面并将脚本插入脚本编辑器Webpart进行测试。
通常在加载DOM之前,我将执行自定义脚本。
<script type="text/javascript">
$(function () {
var sitefrom = window.location.href
var language = sitefrom.search(/portuguese/i); /* IF language = -1, then site from = SPANISH, otherwise site from is PORTUGUESE */
var site_es_pt = sitefrom.replace(/spanish/gi, "portuguese");
var site_pt_es = sitefrom.replace(/portuguese/gi, "spanish");
var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept": "application/json;odata=verbose" };
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,
error: onError
});
})
function onSuccess(data, request) {
var loginName = data.d.Title;
if (loginName == "*@br.XX.com" && language == -1) { alert(loginName); }
if (loginName !== "*@br.XX.com" && language !== -1) { alert(sitefrom); }
}
function onError(error) {
alert("error");
}
</script>
答案 1 :(得分:0)
由于我在加载jquery时遇到问题,因此发现了另一种重定向方式:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
this.clientContext = new SP.ClientContext.get_current();
this.oWeb = clientContext.get_web();
currentUser = this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}
function onQuerySucceeded() {
var loginName = currentUser.get_email();
var user_country = loginName.search(/br.XX/i); /* IF country = -1, then user is SPANISH, otherwise user is PORTUGUESE */
var sitefrom = window.location.href
var language = sitefrom.search(/portuguese/i); /* IF language = -1, then site from = SPANISH, otherwise site from is PORTUGUESE */
var site_es_pt = sitefrom.replace(/spanish/gi, "portuguese");
var site_pt_es = sitefrom.replace(/portuguese/gi, "spanish");
if(user_country !== -1 && language == -1 && loginName !== "xxxxx@br.XX.com") {window.location = site_es_pt;}
if(user_country == -1 && language !== -1 && loginName !== "xxxxx@br.XX.com") {window.location = site_pt_es;}
}
function onQueryFailed(sender, args) {
alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>