title: javascript - redirecting
body: <p>I can get the code to pop-up both alert but redirecting is not working. After adding an item it should redirect.</p>
  
  <pre><code>&lt;script type="text/javascript" src="/_layouts/jquery/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  fields = init_fields();
  // Where to go when cancel is clicked
  goToWhenCanceled = '/test/English/YouCanceled.aspx';
  
  // Edit the redirect on the cancel-button's
  $('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){
      $(this).click(function(){
              STSNavigate(goToWhenCanceled);
        })
  });
  
  // Edit the form-action attribute to add the source=yourCustomRedirectPage
  function setOnSubmitRedir(redirURL){
  var action = $("#aspnetForm").attr('action');
  var end = action.indexOf('&amp;');
      if(action.indexOf('&amp;')&lt;0){
          newAction = action + "?Source=" + redirURL;
      }else{
          newAction = action.substring(0,end) + "&amp;Source=" + redirURL;
      }
  $("#aspnetForm").attr('action',newAction);
  alert(redirURL);
  }
  
  /*
  // Use this for adding a "static" redirect when the user submits the form
  $(document).ready(function(){
      var goToWhenSubmitted = '/test/English/ThankYou.aspx';
      setOnSubmitRedir(goToWhenSubmitted);
  });
  */
  
  // Use this function to add a dynamic URL for the OnSubmit-redirect. This function is automatically executed before save item.
  function PreSaveAction(){
  // Pass a dynamic redirect URL to the function by setting it here,
  // for example based on certain selections made in the form fields like this:
  
      var dynamicRedirect = '/surveys/Pages/ThankYou.aspx';
  
      // Call the function and set the redirect URL in the form-action attribute
      setOnSubmitRedir(dynamicRedirect);
      alert(dynamicRedirect);
      // This function must return true for the save item to happen
      return true;
  }
  
  function init_fields(){
    var res = {};
    $("td.ms-formbody").each(function(){
        if($(this).html().indexOf('FieldInternalName="')&lt;0) return;
        var start = $(this).html().indexOf('FieldInternalName="')+19;
        var stopp = $(this).html().indexOf('FieldType="')-7;
        var nm = $(this).html().substring(start,stopp);
        res[nm] = this.parentNode;
    });
    return res;
  }
  &lt;/script&gt;
  </code></pre>