0

Would like to know how to handle when the page is loaded in one of menu links.

<ul>
 <li><a href="#link1"></a></li>
 <li><a href="#link2"></a></li>
 <li><a href="#link3"></a></li>
</ul>

when the URL starts with #link1 or #link2 or #link3, manipulate:

if('example.com/#link1') {
 alert('link1');
} else if ('example.com/#link2') {
 alert('link2');
} else if ('example.com/#link3') {
 alert('link3');
}
Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
Cristiano Matos
  • 329
  • 1
  • 2
  • 10

2 Answers2

4

It sounds like you need to check the URL fragment in JavaScript. Related answers:

Example (using window.location.hash )

if (window.location.hash === "link1") {
 alert('link1');
} else if (window.location.hash === "link2") {
 alert('link2');
} else if (window.location.hash === "link3") {
 alert('link3');
}
Community
  • 1
  • 1
michaelb
  • 747
  • 3
  • 9
0

If you want to detect hash (#) in the current page url, you should check window.location.hash. It will returns string like #link1 if you are on site.com/#link1 page

Anarion
  • 1,048
  • 6
  • 16