admin管理员组文章数量:1023251
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
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
本文标签: javascriptCannot read property 39attr39 of undefinedStack Overflow
版权声明:本文标题:javascript - Cannot read property 'attr' of undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745508934a2153737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论