-4

I'm trying to route to a dynamic page in JavaScript,

Is there any way I can do this,

localhost/page.html/001

Can I write a code like,

If (url last characters == 001) {
   //Do something
 }
CirLorm
  • 21
  • 8
  • 4
    Possible duplicate of [Get the current URL with JavaScript?](https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript) – Wimanicesir Mar 16 '19 at 18:12

2 Answers2

0
<script type="text/javascript">

var lastUrl = window.location;
lastUrl = lastUrl.replace("localhost/page.html/", "");
if(lastUrl == "001"){
    alert(lastUrl);
}

</script>
ncubica
  • 8,169
  • 9
  • 54
  • 72
kami s
  • 11
  • 4
0

You could use:

const currentUrl = window.location.href

To get the current URL of the side (In this case localhost/page.html/001), then:

const filterUrl = currentURL.split("/");
if (filterUrl[2] === '001') { /* Do stuff */ } else { /* Do other stuff */ }

If your URL's are going to be always like that you could use this little snippet to filter them.

Alberto Perez
  • 2,561
  • 1
  • 14
  • 21