title: Adding onsubmit to a form via javascript
body: <p><strong>How would you go about inserting an OnSubmit attribute to a form via Javascript only?</strong></p>
  
  <p>I'm pretty new to javascript so if you're able to provide detailed example code, that would be most helpful!</p>
  
  <p><strong>Here's the situation:</strong> I'm using a hosted signup page through Chargify (a payments platform) in order to process credit cards for my app, and then send the user back to my own thank you/confirmation page.  </p>
  
  <p>Tracking the entire funnel through google analytics is proving quite elusive due to changing domains (my domain -> Chargify.com -> my domain), since the credit card page is hosted by Chargify on their own domain.  </p>
  
  <p><strong>I'm getting close:</strong> I've been able to get cross-domain tracking working (chargify.com page gets logged in Google Analytics), and can link from my domain to chargify by adding the following onclick attribute to my signup link: </p>
  
  <pre><code>onclick="_gaq.push(['_link', 'http://my-domain.chargify.com/subscriptions/new']); return false;"
  </code></pre>
  
  <p>However, I cannot do the same thing on the way back (Chargify -> Confirmation page) because I do not have access to the Chargify hosted payment page code, and because the user is taken to my confirmation page via a form submission, not a normal link.</p>
  
  <p><strong>Partial Solutions (need your help to finish this up):</strong>
  Chargify allows several options for their hosted pages, one of them being to add custom javascript that gets inserted right before the     <code>&lt;/body&gt;</code> tag in a     <code>&lt;script&gt;</code> tag.  </p>
  
  <p>I found some resources in the Google Analytics documentation on how to link pages, and adding the following to the Chargify form tag might work:     <code>onsubmit="_gaq.push(['_linkByPost', this]);"</code>
  (source: <a href="https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost" rel="nofollow noreferrer">https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost</a>)</p>
  
  <p>The form tag does not currently have an onsubmit attribute, it's just this:     <code>&lt;form action="/my_product/subscriptions" class="new_submission" id="hosted_payment_form" method="post"&gt;</code> </p>
  
  <p><strong>Is there a way to use Javascript to simply append this attribute to the form tag?</strong>  If you'd be able to provide a detailed example of what code I should insert inside of the     <code>&lt;script&gt;</code> tag, that would be extremely appreciated.  </p>