3

I have a basic custom list in SharePoint Online where users have to add items, and I want to make sure that:

  • If user clicked Save, then, User sees: Thanks-Page

  • If user clicked Cancel, then, User sees: Bye-Page

How can I accomplish this with script editor web part on the NewForm.aspx page? Is there a better and fast way?

1

On your Cancel button, in the NewForm.aspx page, you can add the following onclick function:

<button type="button" onclick="goToByePage();">Cancel</button>

<script>

function goToByePage(){
     window.location.assign("https://DOMAIN/PAGEREF/Bye-Page.aspx");
}
</script>

The save button is a little more difficult - normally it automatically refreshes the page with the new item shown in the list. Instead, you want it to re-direct to a thanks page. You could use Marc Anderson's SPServices function SPRedirectWithID.

  • But when I add a script editor web part on the newform.aspx it didn't work for me ? – Mary Larson Aug 16 '16 at 19:38
  • If you have SP Designer I would edit the newform.aspx page directly. I've found that the script editor will sometimes strip JS code out of the page. You can inspect your page with a debugger (F12) to see if your code is executing when you click cancel or save. – Amanda Gray Aug 16 '16 at 19:55
0

I would recommend that rather than Script Editor or editing the page directly that you actually add a Content Editor Webpart. Have it point to a .HTML file that you put in your SiteAssets directory. In the Content Link section of the Content Editor Webpart you can then include the HTML & JavaScript.

To overcome the Save it might actually be easier to just create the link to the list (new form) by creating a link on your site and passing a different source.

IE: if you have a button on the page that goes to http://yourfarm.sharepoint.com/sites/yoursite/Lists/YourList/NewForm.aspx?&Source=/sites/yourSite/Pages/Thanks.aspx

That way when they save it'll dump them off there. The cancel you could do a little bit of jQuery to change the function. I think the one item she was missing is you need to have that function but then there needs to be some code that overrides the default cancel.

<script>
$("input[value='Cancel']").attr("onClick", "goToByePage()");
</script>
  • Not sure I understood. My list is a very basic custom list. so when user add an item the newform.aspx has two buttons: Save and Cancel. If user user clicked Save it goes to Thanks.apx and if clicked Cancel it goes to Bye.asps. That's all ! I tried this c-sharpcorner.com/blogs/… but it doesn't work. Any idea ? – Mary Larson Aug 16 '16 at 21:52
  • Yeah but you need the actual form to save. That script you referenced doesn't include the logic to actually save the column values to the list it just redirects you to a page. That's why I'm suggesting to leave the save button alone and just override the cancel button. If you provide a source in the URL to the newform.aspx it will redirect you to that page on save or cancel. So I'm saying just override the cancel and have it redirect you to a separate page so that both save & cancel don't redirect to the same page. – Jared Matfess Aug 17 '16 at 11:54

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.