
Originally Posted by
wkenny
It does seem a bit odd that js does not have a built-in function to return page name.
It's not the responsibility of the scripting language to provide this sort of feature, but the object model in which it operates. However, given that it's so easy to obtain the value, there really isn't any point for browser vendors to provide it.
Code:
function getFileName() {
var url = document.URL,
i = url.lastIndexOf('/') + 1,
j = url.indexOf('#', i),
k = url.indexOf('?', i);
if(-1 == j) {j = url.length;}
if(-1 == k) {k = url.length;}
return url.substring(i, Math.min(j, k));
}
Note that this will return an empty string when the document was obtained in response to a path-only URL. That is, a URL like
  http://www.example.com/
will return an empty string. The browser has no idea what file is used when returning the HTTP response - that's entirely down to the server and is dependent upon its configuration.
Mike
Bookmarks