locked
Redirect to New URL Script RRS feed

  • Question

  • 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. 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>​​​


    nk

    Wednesday, July 24, 2019 7:26 PM

Answers

  • Hi,

    Simple fix:

    Override "new item" hyperlink in List view page directly with modified Source parameter:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#idHomePageNewItem").attr("href", "http://sp/sites/dev/lists/Testlist/newform.aspx?source=http://sp/sites/dev/SitePages/ThankYou.aspx");
            $("#idHomePageNewItem").attr("onclick", "");
        });
    </script>
    

    Thanks

    Best Regards


    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.

    Thursday, July 25, 2019 2:09 PM

All replies

  • You can check 


    The problem here is with the <button> tag. Its default behavior is to act as a submit button unless otherwise declared and will reload.

    To keep your <button> tag, add type='button' to the button element. I think that prevents the reload.

    Or you could go with the ole <input> tag with a type='button'. That keeps the reload from happening as well.

    Or some other html element with an onclick event will work too.

    https://sharepoint.stackexchange.com/questions/104330/sharepoint-app-auto-refreshes-when-using-button-control


    Please remember to click Mark as Answer on the answer if it helps you

    Wednesday, July 24, 2019 11:02 PM
  • how can i fix this code to make it work?

    nk

    Thursday, July 25, 2019 3:18 AM
  • Hi lahsin,

    This is by design.

    First time page load will set the normal Source parameter in the url when clicking "Add New Item" in list view page:

    NewForm.aspx?Source=http://sp/sites/dev/Lists/RedirectList/AllItems.aspx

    Then in the code snippet, $document.ready function, it will check and override the Source Parameter with new redirect url after saving items.

    It will need to reload the page second time to apply the changes, without reload, it won't be able to override this "Source" querystring, please check the same question here:

    https://sharepoint.stackexchange.com/questions/218234/change-source-parameter-in-url-without-reloading-page

    Thanks

    Best Regards

     

    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.





    Thursday, July 25, 2019 5:39 AM
  • is there an alternate way to fix this? All I am trying to do is redirect when the form is save to a new URL.

    nk

    Thursday, July 25, 2019 1:51 PM
  • Hi,

    Simple fix:

    Override "new item" hyperlink in List view page directly with modified Source parameter:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#idHomePageNewItem").attr("href", "http://sp/sites/dev/lists/Testlist/newform.aspx?source=http://sp/sites/dev/SitePages/ThankYou.aspx");
            $("#idHomePageNewItem").attr("onclick", "");
        });
    </script>
    

    Thanks

    Best Regards


    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.

    Thursday, July 25, 2019 2:09 PM