Upgraded To WordPress 4.5 – Syntax Error, Unrecognized Expression
Upgraded To WordPress 4.5 – Syntax Error, Unrecognized Expression
Quick post regarding a syntax error related to a recent upgrade to WordPress 4.5. I’ve been using this smooth scroll technique from CSS Tricks for the greater part of five years. Today, when upgrading to WordPress 4.5 I noticed the sites scripts.js file was kicking back some new errors. After investigating my console, I saw this particular line:
Syntax error, unrecognized expression: a[href*=#]:not([href=#])
The error is related to the newest version of jQuery that comes pre-packaged with WordPress and thankfully it’s a simple fix. The issue is the selector is not valid within the first line of the below smooth scrolling function:
$(function() { $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); });To fix it, just replace the first line of:
$('a[href*=#]:not([href=#])').click(function() {with:
$('a[href*=\\#]:not([href=\\#])').click(function() {Now we are escaping the special character that is the pound (or hashtag) sign. All set, no more errors!
Share Your Thoughts