Friday, November 16, 2012

JavaScript…Cannot call method match of undefined

I\’m getting this error: Uncaught TypeError: Cannot call method \'match\' of undefined in my JavaScript. I\’m trying to write this without jQuery since it\’ll be the only js on the site.


My code is supposed to add a class, \”active\”, to the <a href=\"...> in the navigation that links to the current page.


I\’m guessing it might be the contentLoaded function?….source


Here\’s my code…(error occurs on line 9)…fiddle



(function(window, document, undefined) { contentLoaded(window, function() {

var page = document.location.pathname.match(/[A-z]+$/)[0],
nav_anchors = document.getElementsByTagName(\'header\')[0]
.getElementsByTagName(\'nav\')[0]
.getElementsByTagName(\'a\');

for(i in nav_anchors)
if(nav_anchors[i].href.match(/[A-z]+$/)[0] = page) //LINE 9 <-- Error
nav_anchors[i].classList.add(\'active\');

})
})(this, this.document)


Thanks!






Rating: 2 out of 5 based on 4 ratings



The post JavaScript…Cannot call method match of undefined appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-cannot-call-method-match-of-undefined.html

No comments:

Post a Comment