最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'attr' of undefined - Stack Overflow

programmeradmin1浏览0评论

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 return undefined, hence calling attr() 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
 |  Show 1 more ment

2 Answers 2

Reset to default 1

If 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
发布评论

评论列表(0)

  1. 暂无评论