If you look at below code, I have used executeordelayuntillscriptloaded at start of my script so that it will run only after that file is loaded. But this breaks some functionality. Can you check this code and tell me if its not good practice?
It was done so that the SP.Utilities.Utility.getLayoutsPageUrl functionality never throw undefined error:
<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
ondemand="false" localizable="false" loadafterui="true" />
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
ondemand="false" localizable="false" loadafterui="true" />
<script type="text/javascript">
SP.SOD.executeOrDelayUntilScriptLoaded(runthiscode, 'SP.js');
function runthiscode()
{
var personProperties;
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
function getUserProperties() {
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
personProperties = peopleManager.getMyProperties();
// Load the PersonProperties object and send the request.
clientContext.load(personProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
if (personProperties.get_userProfileProperties().CustomPropertyValue == "False") {
window.location(SP.Utilities.Utility.getLayoutsPageUrl('')+'someurl.aspx');
}
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
var r = confirm("There was an error, the page will reload");
window.location(SP.Utilities.Utility.getLayoutsPageUrl('')+'someurl.aspx');
}
function someotherfunction() {
window.location.href = SP.Utilities.Utility.getLayoutsPageUrl('')+'someotherurl';
}
}
</script>