do not call javascript if use browser back button

Posted on 2012-08-23
Last Modified: 2012-08-24
I have the following Bronto javascript code in order to track the conversion. The problem I have is when customer use "Back" button in browser, then the script will be called again, then tracking will be counted one more time, I only need to track once. Not when customer user "Back" button.

createItemObjects is basically get information from purchaseTable (html) on the same page. I tried to use $(function(){ $('#orderInfoHidden').remove(); }); I'm trying to remove the table. But It's not working. I think it's because the html does not really change. Back button will make javascript do it all over again.

var createItemObjects = function(purchaseTable) {
    var trs = purchaseTable.rows,
        trl = trs.length,
        i = 0,
        j = 0,
        keys = [],
        item, items = [];
	// Loop over each row.
    for (; i < trl; i++) {
		// Assume the first row is for headers
        if (i == 0) {
			// Create our keys using the TH ids
            for (; j < trs[i].children.length; j++) {
		// All other rows, create an item object using
		// the TH ids as keys and each TD text node as values
        } else {
            item = {};
			// Set j counter back to 0
            for (j = 0; j < trs[i].children.length; j++) {
                item[keys[j]] = trs[i].children[j].innerHTML;
			// Add the item object to the the items array
   return items;
	// This example returns:
	//	 {"item_id":"TEC123","desc":"arts and crafts","amount":"3","quantity":"1"},
	//	 {"item_id":"TEC234","desc":"reading","amount":"1","quantity":"1"}

// Get the order_id
var orderId = document.getElementById('orderId').innerHTML;

// Javascript Conversion Tracking example method
// NOTE: The item_id cannot contain spaces!
// NOTE: If no date is provided, the current date/time is used.
bta.addConversion({ "order_id": orderId, "date": "", "items": createItemObjects(document.getElementById('order_confirmation'))

Open in new window

Question by:xiaoyunwu
    LVL 9

    Expert Comment

    by:Evan Cutler
    LVL 1

    Accepted Solution

    Record that user has been to a page in a cookie

    Alter your script to only run the track if cookie does not exist.

    Author Closing Comment

    Great idea, alansjunk. It worked.

    When user goes to order confirmation page, I delete the conversion cookie using the server side code. (this ensures when another order is made, conversion code will be called) Then render the html page with javascript. Conversion code will only run when cookie is not there and after run will set the cookie. When user use back button, only client side javascript will run and check cookie.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now