1

I have the the url http://ste.com/members/username/, I had search stackoverflow for the following code which is great. but I face the issue is that, I cannot change the username all the time, so I cannot put the fixed username. I was trying to put 'members',but it will affect other pages. So how can I check if my url contain any string after members/, thanks!

if (window.location.href.indexOf("username") > -1) {
    $('html, body').animate({
        scrollTop: $('#item-nav').offset().top
    }, 'slow');
}

2 Answers 2

4

I'd use regex here.

if (window.location.href.match(/members\/\w+/))  {

This checks if the href matches the string 'members/' and then at least 1 word character after that.

You can read more about regular expressions here https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

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

Comments

1

This is not a generic and appropriate fix. But you can try this way also using substring().

var link = window.location.href;
if(((link.substring(link.indexOf("members/") +8)))){
    //your code
   }

Hope this helps!

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.