javaScript will not fire unless the users do a hard re-fresh (ctrl F5) for the page
-
29-09-2020 - |
Pregunta
I have added the following script to my EnterpriseWiki.aspx page layout inside an enterprise wiki site collection:-
<script type="text/javascript">
_spBodyOnLoadFunctions.push(function(){
ExecuteOrDelayUntilScriptLoaded(showStatusAlert, "sp.js");
})
function showStatusAlert(){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(_spPageContextInfo.pageItemId);
ctx.load(list);
ctx.load(item);
ctx.executeQueryAsync(
function(){
var status = item.get_item('_ModerationStatus');
// 2 - rejected
if(status == 1){
statusDesc = SP.UI.Status.addStatus("Warning, this page has been rejected, Please view the page history to check for the latest approved version.!");
SP.UI.Status.setStatusPriColor(statusDesc, 'red');
}
}
, function(err){
});
}
</script>
</asp:Content>
the script will check if the page status is rejected and display a red alert. now when the user click on the page link he will not see the red alert , as follow:-
but when he refreshes the page he will be able to see the alert as follow:-
can anyone advice what is causing the script to not run on the first page load?
Solución
I think the issue is the value your retriving is not getting it at the correct moment first time round. Thats why on the second postback it would get the correct item value and its loaded within the context properly. have you tried calling the method out of the page load event? so at bottom code just do showStatusAlert();
instead.
or
use this method instead, it loads the list item id on context load
http://hameersaleem.blogspot.co.uk/2012/09/getting-page-field-value-in-custom.html
so would look somthing on the lines of:
<script type="text/javascript">
var pageItem; //variable for list item of page being displayed (which uses this page layout)
var pageFieldNameVar = '_ModerationStatus'; //variable for page field to be retrieved
var context; // This variable will contain SharePoint Client Context
function showStatusAlert(pageFieldName){
//this is the part that you need that you dont have in reloading the page item on first initial load
if (pageItem == null)
setPageContext();
//explicitly requesting to load the field Name for the page item
context.load(pageItem, pageFieldName);
//making the actual request
context.executeQueryAsync(Function.createDelegate(this, function () { onPageFieldSuccess(pageFieldName); }), Function.createDelegate(this, function () { onPageFieldFailed(pageFieldName); }));
}
//sets the pageItem global variable to establish the context for current page
function setPageContext() {
//_spPageContextInfo is defined in every SharePoint page and has pageListId and pageItemId
//properties populated in publishing pages
var pageListId = _spPageContextInfo.pageListId;
var pageItemId = _spPageContextInfo.pageItemId;
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
//getting the list item for the current page
var webLists = web.get_lists();
var pageList = webLists.getById(pageListId);
pageItem = pageList.getItemById(pageItemId);
}
//this function is called automatically when request made by getPageField function is successful
function onPageFieldSuccess(pageFieldName) {
//retreiving the page field value from page list item
var fieldVal = pageItem.get_item(pageFieldName);
// 2 - rejected
if(fieldVal == 1){
statusDesc = SP.UI.Status.addStatus("Warning, this page has been rejected, Please view the page history to check for the latest approved version.!");
SP.UI.Status.setStatusPriColor(statusDesc, 'red');
}
}
//this function is called automatically when request made by getPageField function is unsuccessful
function onPageFieldFailed(pageFieldNameVar) {
//do something to display error
}
//calling only getPageField function. This will eventually call other functions eventually rendering the links
_spBodyOnLoadFunctions.push(function(){
ExecuteOrDelayUntilScriptLoaded(showStatusAlert(pageFieldNameVar)
, "sp.js");
})
</script>
</asp:Content>