Forum Moderators: phranque

Message Too Old, No Replies

Sometimes, just sometimes, I hate my job

         

csdude55

6:36 am on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For awhile now I've seen this same error in the console:

Uncaught TypeError: Cannot read property 'defaultView' of undefined
at $e (jquery.min.js:2)
at Fe (jquery.min.js:2)
at Function.css (jquery.min.js:2)
at jquery.min.js:2
at z (jquery.min.js:2)
at w.fn.init.css (jquery.min.js:2)
at HTMLDocument.<anonymous> (<anonymous>:3:24)
at l (jquery.min.js:2)
at c (jquery.min.js:2)

The error cites jquery.min.js, and I don't use "defaultView" anywhere in my code, so I assumed it wasn't anything in my own code. And it's not a fatal error, so not really a big deal. But today I had a little free time, so I decided to see if I could track it down. Google didn't really give me anything except for a few vague references to possibly referring to something that didn't exist or was null, so that wasn't a lot of help.

The error is on every page of the site except for the homepage, so step one, narrow it down to Javascript that's on every page... that's my universal .js file, easy enough. So I commented out a huge block of script, uploaded it, and voila, no more error! Yay! So the error is obviously within that big chunk, right?

So I started uncommenting sections at a time, in an attempt to narrow it down even further.

After about 30 minutes of this, I finally had it narrowed down to 3 lines:

// "scroll" is a predefined object
var sectionHeight, asideHeight;

if (scroll.section && scroll.aside) {
sectionHeight = $(scroll.section).height() + 800;
asideHeight = $(scroll.aside).height();
}


So I change it to this:

var sectionHeight =
asideHeight = 0;

if (scroll.section && $(scroll.section).height())
sectionHeight = $(scroll.section).height() + 800;

if (scroll.aside && $(scroll.aside).height())
asideHeight = $(scroll.aside).height();


Yay, no more error!

Then, 3 more pageviews later... error's back. Exact same error, with no guidance on where it might be originating >:-(

So I've ended up spending the day working on an error that may not even be on my end, and that's not even important, just cause it bugs me.

lammert

9:09 am on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Replacing jquery.min.js with its non-minified version will give you more information about the line where the error is generated.

My experience is that this error may be triggered by a construction like $("#tag").somefunction(this.somefield); where the execution is only valid after the DOM has been loaded and #tag has been defined. Depending on the sequence of events, this may cause the error to trigger in some circumstances and not in others. These problems are very hard to find and caused by the asynchronous nature of building the DOM.

tangor

9:37 am on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



just cause it bugs me


That's reason enough to expend the time. Good coders do this. Why? Error free is bullet proof!

Keep after it!

csdude55

4:42 pm on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the tips, @lammert :-) I figured it had something to do with a use of this instead of $(this) somewhere, too, but I haven't been able to find it. I'll have to take your advise and switch away from minified to see if I can find it.

BTW, is your username with a lowercase L or an uppercase i? I haven't been able to figure it out so I have to copy 'n' paste your username when I @ you...

Days like yesterday make me really feel like the worst coder in the world! I'll be honest, this is mostly a field filled with extremely arrogant know-it-alls that tend to make me feel stupid, anyway... I've been coding for nearly 25 years, but I'm entirely self taught so there is still a loooooot that I don't know or understand. It's very frustrating, and when I spend all day (turning in to 2 days) on something like that it really makes me question my career choices. I suspect that a better, more educated coder would have found it in 5 minutes.

iamlost

8:33 pm on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I’ve listened to zillions of jQuery is undefined complaints from friends and colleagues over the years, it must be the among the most common most irritating of bugs; the common cold of coding.
Note: I only played a very little with js libraries and frameworks and have almost no js at all these days.

That said, if also calling js other than jQuery:
Check js load order, move jQuery to first to eliminate possible calls to it before its loaded.

Check if other js is reassigning variable or using out of scope.

Regardless, best wishes.

csdude55

9:03 pm on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I sorta regret pulling in jQuery, but I'm also convinced that infinite scroll will increase my ad impressions so I hope all this work pays off!