If you're not going to assign the onclick
inline in the element, I see no reason why you wouldn't just use a standard event handler. You can read more about the benefits here.
I'd suggest this:
$("a").click(function() {
var href = $(this).attr("href");
return goog_report_conversion(href);
});
Also, your first two lines of code are performing the same task, so you won't need both. (More info here)
jQuery(function($){ ... });
// OR
$(document).ready(function() { ... });
If you're wondering why yours doesn't work:
attr()
doesn't have its own this
arguments.
(Tracing it upwards, the context in which you're using it would actually refer to the document
, as the scope is defined by $(document).ready( ... )
)
If you were to do it that way, you'd likely need to modify your goog_report_conversion()
function to accept a parameter of the clicked element, rather than the href
. Your this
would be included in the string, not concatenated. Using the passed element, you'd fetch the href
similarly to how we did in the example above.
function goog_report_conversion(clicked_element) {
var href = $(clicked_element).attr("href");
//do something with href
});
$("a").attr("onclick", "return goog_report_conversion(this);");
That being said, please don't do this.