The-implementation-of-the-selected-state-of-the-node-js-routing

  • Obtain the current URL with the JavaScript API window.location.href of the original soundtrack
  • Intercepting the parameters of URL by split method
  • You can pass arguments at the back of the route
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>navigation active</title>
</head>
<body>
<nav >
<ul id="navigation">
<li ><a href="/">Home</a></li>
<li ><a href="/about">About</a></li>
<li ><a href="/help">Help</a></li>
<li ><a href="/contact">Contact</a></li>
<li ><a href="/tems">Terms</a></li>
</ul>
</nav>
<script type="text/javascript">
(function(){
var navigation = document.getElementById("navigation");
var navigation_node_li = navigation.getElementsByTagName('li');
var navigation_node_a = navigation.getElementsByTagName('a');
var index = 0;
var url = window.location.href.split('?')[0].split('/').pop();
if(url){
for (var i=navigation_node_a.length; i--;) {
if(navigation_node_a[i].href.indexOf(url) !== -1){
index = i;
break;
}
}
}
navigation_node_li[index].className = 'active';
})();
</script>
</body>
</html>