BillyW
BillyW

Reputation: 143

window.location.href.match Not Working

Here is the code:

$(function() {

    var pathname = (window.location.href.match(/[^\/]+$/)[0]);

    $('#menu dl dd a#toparrow').each(function() {
        if ($(this).attr('href') == pathname) {
            $(this).addClass('currenttop');
        }
    });
});

Here is a link to my website: http://bluraymoviestore.com

The purpose of the script is to make the arrow image stay on the current page, like it does when you first click to the categories.

The script works when I'm clicked on the parent page, but when I click to the next page under the same category, the script no longer works (example: works with /bluraynewreleases but not /bluraynewreleases-2625374011-rc-2-new_releases.html). What do I need to add to the code to make it work?

Upvotes: 1

Views: 4820

Answers (1)

Daniel Imms
Daniel Imms

Reputation: 50229

Your regex doesn't match anything so it throws an error when you attempt to access [0] on it. Assuming your regex is correct this should fix the issue.

$(function(){
    var matches = window.location.href.match(/[^\/]+$/);
    if (matches && matches.length) {
        var pathname = (matches[0]);

        $('#menu dl dd a#toparrow').each(function() {
            if ($(this).attr('href') == pathname) {
                $(this).addClass('currenttop');
            }
        });
    }
});

Upvotes: 1

Related Questions