Redirect to New URL Script

Hello I have the code below that allows you to redirect to a new URL when the form is save. It works but whenever click add new item it reloads the page twice in SHAREPOINT. Can someone help me fix this?

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script> ​​​<br/><script type="text/javascript">

$(document).ready(function(){

$("input[value='Cancel']").attr('onclick','');

$("input[value='Cancel']").click(function(){

location.href = _spPageContextInfo.webAbsoluteUrl; // It will redirect you to home page after clicking on cancel button

});

var stringURL = location.href;
if(stringURL.indexOf("AllItems")>-1){
stringURL = stringURL.replace(stringURL.substring(stringURL.indexOf("Source="),stringURL.length),'Source='+_spPageContextInfo.webAbsoluteUrl+'/SitePages/ThankYou.aspx');
location.href = stringURL;
}

if(!(stringURL.indexOf("Source=")>-1)){
location.href = location.href+"?Source="+_spPageContextInfo.webAbsoluteUrl+'/SitePages/ThankYou.aspx';

}

});

</script>​​​

Hi @surajkay19, then your script probably runs into one of the two if conditions after the redirect, causing another redirect… what is the value of _spPageContextInfo.webAbsoluteUrl here?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.