I have the following JS, my alert statement returns undefined. However if I place it within the function it returns the correct value. How can I pass the variable accName outside of the function so that I can use it elsewhere?
Thanks in advance.
<script src="/codelibrary/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>
<script src="/codelibrary/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var accName;
GetUser();
alert(accName);
});
function GetUser(accName) {
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) {
displayName = data.d.Title;
loginName = data.d.LoginName;
accName1 = loginName.split('|')[2];
accName = accName1;
return accName;
}
function onError(error) {
alert("error");
}
</script>