Sometimes, we want to get the name of an HTML page in JavaScript.
In this article, we’ll look at how to get the name of an HTML page in JavaScript.
How to get the name of an HTML page in JavaScript?
To get the name of an HTML page in JavaScript, we can use the split
and slice
methods.
For instance, we write
const fileName = location.href.split("/").slice(-1);
to get the current page URL with location.href
.
Then we call split
with '/'
to return an array of URL segments.
And we get the last segment, which has the file name with slice
called with -1.
Conclusion
To get the name of an HTML page in JavaScript, we can use the split
and slice
methods.