Adding onsubmit to form via javascript

How could you insert the OnSubmit attribute into the form only through Javascript?

I am new to javascript, so if you can provide detailed sample code, that would be very helpful!

Here's the situation: I use the hosted registration page via Chargify (payment platform) to process credit cards for my application, and then send the user back to my own thanks / confirmation page.

Tracking the entire sequence through Google Analytics is rather elusive due to domain changes (my domain → Chargify.com → my domain), as the credit card page is hosted in Charged in its own domain.

I'm getting closer: I was able to track cross-domain tracking (the chargify.com page is registered with Google Analytics) and can contact my domain to charge a fee by adding the following onclick attribute for my registration link:

onclick="_gaq.push(['_link', 'http://my-domain.chargify.com/subscriptions/new']); return false;" 

However, I can’t do the same on the way back (“Charging → Confirmation” page) because I don’t have access to the payroll page code of the board, and because the user is sent to my confirmation page via the send form, not a normal connection.

Partial solutions (your help is needed to complete this): Chargify allows several options for placing hosted pages, one of which is adding custom javascript, which is inserted immediately before the </body> tag in the <script> .

I found some resources in the Google Analytics documentation on how to link pages, and adding the following to the payment form tag may work: onsubmit="_gaq.push(['_linkByPost', this]);" (source: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost )

There is currently no onsubmit attribute in the form tag, it's simple: <form action="/my_product/subscriptions" class="new_submission" id="hosted_payment_form" method="post">

Is there a way to use javascript to simply add this attribute to the form tag? . If you can provide a detailed example of what code I should insert inside the <script> tag, which would be extremely appreciated. Thanks!

+4
source share
1 answer
 window.onload = function() { var form = document.getElementById('hosted_payment_form'); form.onsubmit = function() { _gaq.push(['_linkByPost', this]); } } 

I believe the above example is similar to what you need. We use document.getElementById to get a link to your form. Then set the onsubmit property to the code that you want to execute before submitting the form. Remember to put this inside the onload event of the window if this JavaScript is executed before the page is rendered to ensure that the form is created.

+6
source

All Articles