Could someone recommend a way to get page name from a url using JavaScript?
For instance if I have:
http://www.cnn.com/news/1234/news.html?a=1&b=2&c=3
I just need to get "news.html" string
Thanks!
Answer: Use the window. location. href Propertyhref property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.
var lighboxHeight = (pagenumber-1)*window.
The window.location.href property returns the URL of the current page.
You can do this pretty easily via window.location.pathname
parsing:
var file, n;
file = window.location.pathname;
n = file.lastIndexOf('/');
if (n >= 0) {
file = file.substring(n + 1);
}
alert(file);
...or as others have said, you can do it with a regexp in one line. One kinda dense-looking line, but with a comment above it, should be a good way to go.
I think it's
window.location.pathname.replace(/^.*\/([^/]*)/, "$1");
So,
var pageTitle = window.location.pathname.replace(/^.*\/([^/]*)/, "$1");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With