potential bug in .val() element for non-form elements, new in 1.2
This is a cross-post from the general list, where I first mentioned
the issue.
I have discovered a potential bug, or at least difference between 1.1
and 1.2, when using the .val() method. Previously, when running this
on a non-form element (usually a link), it would return null or
perhaps undefined (a 'falsy' value). Now it generates an error which
halts script execution.
Below is a stripped-down example of how I am using it in my code. The
line which breaks is:
item: $.trim($(event.target).attr("src") || target.val() ||
target.text()),
To reproduce the error, include this script on any page, and place a
breakpoint on the send(params) statement. The problem can be seen
when any link is clicked: the breakpoint is never reached.
My simple workaround has been to swap the target.text() and
target.val() positions in the problematic line.
oliver
// example of potentially-broken use of .val() on non-form elements
Analytics = new (function() {
var beacon = "beacon.gif";
// get the page load time
var start_time = new Date().getTime();
// click handler for the document, filters to all links/maps/submit/
image buttons
this.navigation = function(event) {
try {
var target = $(event.target).add($
(event.target).parents()).filter("[@href], :submit, :image").filter(":first");
// only beacon the above items, or if we were given a specific
destination
if (!target.length) { return; }
var params = {
type: "navigation",
page: location.pathname,
// an image source, button value, or link text
item: $.trim($(event.target).attr("src") || target.val() ||
target.text()),
// link href, or form action
destination: target.attr("href") ||
target.parents("form").filter(":first").attr("action")
};
send(params);
} catch(e) {
console.log("analytics failed on target: " + event.target + "\n" +
e);
} finally {
event.stopPropagation(); // for debug only
event.preventDefault(); // for debug only
}
}
// sends a beacon, returns when the request is sent (not returned) or
after a short timeout
var send = function(params) {
$.extend(params, {
time_on_page: ((new Date).getTime() - start_time) / 1000,
random: Math.round(Math.random() * 100000000)
});
var timeout = (new Date()).getTime() + 500;
var request = $.ajax({ data: params, url: beacon });
while (true) {
if (request.readyState >= 2) break;
if ((new Date()).getTime() >= timeout) break;
}
}
// apply navigation tracking to the whole document: these are
filtered on click
$(document).click(this.navigation);
});