locked
submitting survey results automatically when user goes out of the page RRS feed

  • Question

  • I saw an article that show how to submit survey results automatically when user goes out of the page.

    I copied this code to newform.aspx right after the </table> tag:

     

    <script type="text/javascript">

    function findFinish(which)
    {
       var btn = null;
       var count = 1;
       var e = document.getElementsByTagName("input");

       for(var i = 0; i < e.length; i++)
       {
          if (e[i].type == 'button' && e[i].value == 'Finish')
          {
             if (count == which)
             {
                btn = e[i];
                break;
             }
             else
             {
                count++;
             }
          }
       }
     
       return btn;
    }
    function insertSubmitEvent()
    {
       var btn = findFinish(1);
       if (btn != null)
       {
          if (typeof btn.onclick == "function")
          {
             var oldClick = btn.onclick;
             btn.onclick = function() { window.onbeforeunload = null; oldClick(); }
          }
       }
     
       var btn2 = findFinish(2);
       if (btn2 != null)
       {
         if (typeof btn2.onclick == "function")
         {
            var oldClick = btn2.onclick;
            btn2.onclick = function() { window.onbeforeunload = null; oldClick(); }
         }
       }

    }
    function forceSubmit()
    {
       alert('Since you clicked off the page, your answers will now be recorded – you were warned!');
       var btn = findFinish(1);
       if (btn != null)
         btn.click();
    }
    insertSubmitEvent();
    window.onbeforeunload = forceSubmit;
    </script>

     

     

    the code is taken from the article:

    http://vspug.com/joed/2008/09/22/Using-Sharepoint-Surveys-for-a-Quiz/

     

    it shows the alert, but the answers doesn't saved.

    Do you recognize an mistake in this script?


    • Edited by yoka791 Thursday, October 6, 2011 5:48 PM
    Thursday, October 6, 2011 5:47 PM

Answers

  • Hi yoka791,

    I have a test in my local server with the code you provide, it works well.

    To add script code like this, you need to add them to the right place.

    Please find line: <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">(code after  this line will be table layout by default), put the code under this line, and check whether it works.

    Thanks,
    Qiao

    Tuesday, October 11, 2011 2:43 AM
    Moderator