0

I'm using the following JavaScript to get the pathname to show a link is selected:

var pathname = window.location.pathname;

    $(document).ready(function ()
    {

        $('ul#ui-ajax-tabs li a').each(function()
        {
            if ($(this).attr('href') == pathname)
            {
                $(this).parents('li').addClass('selected');
            }
        });
    });

However it has problems if that URL has extra parameters such as / on the end of the url or additional parameters (see examples below) Is it possible to check if the link matches part of the url so for example:

The Link is: /Organisations/Journal/

And the Current page is: /Organisations/Journal/Edit/21

This would class as being selected as it matches part of the url!

1

2 Answers 2

4
window.location.pathname.match('^/Organisations/Journal/');

The ^ character matches the beginning of the string.

Sign up to request clarification or add additional context in comments.

Comments

0

You can probably use indexOf to check if the pathname starts with the value of the href attribute:

if (pathname.indexOf($(this).attr('href')) == 0) {
    ...
}

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf for more information on the indexOf method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.