I have a menu with links that may look like one of the following:
mywebsite.com/#find
mywebsite.com/about-us/#team
mywebsite.com/#social
mywebsite.com/services/#something
I want to do something only to the first and third links (the ones that don’t have a subdirectory in the url path). How do I check if a # hash is the first element after the first slash in the link?
$('#menu a').click(function() {
var target = this.href;
// check if "/#" is the first thing after the domain name
});
Thank you.
2
Answers
Let’s first ignore the
https://
part then find the first/
and hope to find a#
after it.The
URL
class that parse URLs can help you.URL.pathname
property contain path of url (string after domain)More accurate mode is