Menu

Get page or file name from URL using JavaScript

Read the path of the current browsing page from DOM using the below JavaScript statement. Let's assume the URL of the page is "https://rashidjorvee.blogspot.com/2020/07/remote-debugger-in-eclipse.html"

var path =window.location.pathname;


After running the above statement value of path will return "/2020/07/remote-debugger-in-eclipse.html"

Now, split the path with slash(/) to get the last occurrence, which will be the final page name URL.

var page = path.split("/").pop();

After running the above statement the value of the page will be "remote-debugger-in-eclipse.html"

Now, if we want to remove the extension .html then run the below statement which will manipulate the value of the page variable.

var name = page.substr(0, page.lastIndexOf('.'));

After running the above statement the value gets assigned to the name variable "remote-debugger-in-eclipse"

No comments:

Post a Comment