2

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.net/4ocdcqrf/

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • 2
    jQuery.url is undefined if you do a console log(jQuery.url) you will see it's undefined – Radu Feb 19 '16 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 Feb 19 '16 at 14:40
  • I don't think `.attr` is one of `$.url`'s methods. – Steve Perry Feb 19 '16 at 14:40
  • Thanks for the feedback/ I'm using this tutorial here: https://www.distilled.net/blog/conversion-rate-optimization/using-jquery-and-google-analytics-events-to-track-form-abandonment/ – michaelmcgurk Feb 19 '16 at 14:41
  • From that guide: `I’ve also used a jquery url parser, the installation of which was very simple`. They link to the guide, but it 404s. That's the issue; you're missing that library. – Rory McCrossan Feb 19 '16 at 14:43
  • 1
    I believe the OP needs to make use of this library: https://github.com/allmarkedup/purl – shrmn Feb 19 '16 at 14:43

2 Answers2

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

Community
  • 1
  • 1
Radu
  • 1,047
  • 12
  • 34
0

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
GMchris
  • 5,439
  • 4
  • 22
  • 40