Why am I getting this error on my code?
Cannot read property 'attr' of undefined`
$(document).ready(function() {
var currentPage = jQuery.url.attr("path");
$(':input').blur(function () {
if ($(this).val().length > 0) {
pageTracker._trackEvent("Form: " + currentPage, "input_exit", $(this).attr('name'));
}
});
});
My fiddle: /
Why am I getting this error on my code?
Cannot read property 'attr' of undefined`
$(document).ready(function() {
var currentPage = jQuery.url.attr("path");
$(':input').blur(function () {
if ($(this).val().length > 0) {
pageTracker._trackEvent("Form: " + currentPage, "input_exit", $(this).attr('name'));
}
});
});
My fiddle: https://jsfiddle/4ocdcqrf/
Share Improve this question edited Feb 19, 2016 at 14:41 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Feb 19, 2016 at 14:37 michaelmcgurkmichaelmcgurk 6,53525 gold badges101 silver badges197 bronze badges 6- 2 jQuery.url is undefined if you do a console log(jQuery.url) you will see it's undefined – Radu Commented Feb 19, 2016 at 14:39
-
What is
jQuery.url
? Are you using a 3rd party library as that's not a a standard property. What value are you expecting it to return. By default it will returnundefined
, hence callingattr()
on that is throwing the error. – Rory McCrossan Commented Feb 19, 2016 at 14:40 -
I don't think
.attr
is one of$.url
's methods. – Steve Perry Commented Feb 19, 2016 at 14:40 - Thanks for the feedback/ I'm using this tutorial here: distilled/blog/conversion-rate-optimization/… – michaelmcgurk Commented Feb 19, 2016 at 14:41
- 1 I believe the OP needs to make use of this library: github./allmarkedup/purl – shrmn Commented Feb 19, 2016 at 14:43
2 Answers
Reset to default 1If you do a console log on jQuery.url, you will see this is has a value of undefined.
you will needd to use location.pathname instead of jQuery.url.
Please take a look here for proper understanding
The issue is that jQuery.url
is undefined. I'm not sure where you even got the idea that this property should exist, since it's not part of jQuery. To get the actual current page, you can simply get the native property
window.location.href