• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 803
  • Last Modified:

do not call javascript if use browser back button

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++) {
                keys.push(trs[i].children[j].id);
            }
		// 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
            items.push(item);
        }
    }
   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

0
xiaoyunwu
Asked:
xiaoyunwu
1 Solution
 
Evan CutlerCommented:
0
 
alansjunkCommented:
Record that user has been to a page in a cookie

Alter your script to only run the track if cookie does not exist.
0
 
xiaoyunwuAuthor Commented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now