none
Script Causing Page To Refresh twice RRS feed

  • Question

  • Hi The code below is casuing the page to refresh twice when load the new form, does anyone know how to fix? Also how can I go to a new link when cancle is clicked instead of going back -1?

    Code:

    </style><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>


    nk

    Friday, July 19, 2019 3:18 PM

All replies

  • Help anyone?

    nk

    Friday, July 19, 2019 10:35 PM
  • Hi Iahsin,

    First what is the behavior of this code ? could you provide more details on where to implement and how should it behave so, that i could implement on my end and troubleshoot.

    Thanks & Regards,


    sharath aluri

    Saturday, July 20, 2019 1:40 PM
  • Hi There ,

    I believe Cancel is a button . If so in the Button Tag include 'OnClientClick'= '<Function Name >' . This would ensure that the location.href happens only on the client side

    Regards,

    JP

    Sunday, July 21, 2019 11:18 AM
  • Hi,

    Below logic will cause url redirect(should be same page, that’s why form refresh twice).

    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 you want to redirect to other page when save item, try pushState function.

    <script type="text/javascript">

            function PreSaveAction() {           

                    window.history.pushState('displayform', 'displayform', 'https://tenant.sharepoint.com/sites/lee/Lists/MyList/NewForm.aspx?Source=https://tenant.sharepoint.com/sites/lee/Lists/MyList/DispForm.aspx?ID=9');

                return true;

            }

        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, July 22, 2019 1:57 AM
  • Hi this was implement on sharepoint form, I added the code to the script editor so whenever i save the form it goes to a new URL but everytime i load the form it reloads the page twice. 

    nk

    Monday, July 22, 2019 1:09 PM
  • Hi,

    I know you’re customizing the list form,  PreSaveAction will be call when you submit data in list form(new/edit).

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 23, 2019 7:51 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, so it may help others who has similar requirement.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, August 6, 2019 2:16 AM