Categories
JavaScript Answers

How to get the name of an HTML page in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *