0

I am having an issue about redirecting users to a different URL on Save or Cancel button. First of all I have used below code to change SharePoint list new item form's Save button to Submit.

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("ChangeSPSavetoSubmit()");
function ChangeSPSavetoSubmit()
{
var inputs = document.getElementsByTagName("input");

for(i = 0; i<inputs.length; i++)
 {
    if(inputs[i].type == "button" && inputs[i].value == "Save")
     {
      inputs[i].value = "Submit";
     }
  }
 }
</script>

Above is a working solution, its added as a script editor web part on new item form. Below is the code I added as well on the new item form to redirect users to a different page/URL on Submit/Cancel button clicks.

<script src="/SiteAssets/jquery.SPServices-0.7.2.min.js" type="text/javascript"></script>  
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script type="text/javascript">      

$(document).ready(function() {
     $('input[value=Submit]').click(function() {     
        window.location.replace("https://***.sharepoint.com");  
      });   
     $('input[value=Cancel]').click(function() {       
        window.location.replace("https://***.sharepoint.com");  
      });  
    });
</script>  

The functionality works when clicked on Cancel button but not when clicked on Submit button, can someone please help with the correct code. Thanks in advance, FYI I am working with O365/SPOnline.

1

Actually, the issue is not related to your code, it's related to the default behavior of New item Link.

When you click on the new item link the page redirects to the NewForm.aspx with the ?Source= query string holds the URL of ALLItems view URL, it is the page URL that you come from.

So to handle the redirect after clicking the Submit button, you should handle the New item link rather than the Submit button.


  1. Open and Edit All items page,

  2. Add Script Editor with the below script

Script

<script type="text/javascript">
  $(document).ready(function() {

   document.getElementById("idHomePageNewItem").onclick = function() {
window.location.href= "http://ListURL/NewForm.aspx?Source=http://the reirect url"; 
   }
 });
</script>
0

Try the following script to redirect the page to another page after submitting item.

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var button = $('input[name$="SaveItem"]');  
button.removeAttr("onclick");  
button.click(function() {  
    var elementName = $(this).attr("name");  
    var aspForm = $("form[id=aspnetForm]");  
    var oldPostbackUrl = aspForm.get(0).action;  
    var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl);  
    var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "https://<page>");
    if (!PreSaveItem()) return false;  
    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
});
});
</script>
  • Hi Joanna, thanks so much for the script. I tried it but for some reason its not working. i simply changed the url part to www,google.com. Do I have to make any other changes? Please let me know, thanks. – mdevm Nov 6 '18 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.