Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract html page name from a Page - Location

Status
Not open for further replies.

vvicin01

Programmer
Aug 14, 2001
21
0
0
US
I need to extract the html page name from a page using javascript. I tried location.pathname and others but none will extract just the name of the html page. Also, can this be done inside a frameset? Thank you
 
This should give you just the name of the html file that you are viewing, and yes it will work in frames:
Code:
<script language=JavaScript>
function getLocation() {
   var str = String(window.location);
   return str.substr(str.lastIndexOf("/") + 1, str.length - str.lastIndexOf("/"));
}
</script>
<body>
<form name=blahForm>
<input type=button value='click me' onclick='alert(getLocation())'>
</form>
</body>

-kaht

banghead.gif
 
You started off on the right foot with location.pathname. You then need to take this string and trim it down to just the file. Try this as a solution...

Code:
<html>
<head>
<script type="text/javascript">
function testURL()
{
	var myPath = location.pathname;
	if (myPath.lastIndexOf("/") > -1) return (myPath.substr(myPath.lastIndexOf("/")+1));
	return (myPath);
}
</script>
</head>
<body>
<a href="javascript:alert(testURL());">Test</a>
</body>
</html>

If you were to put this on your web server at the following URL:
Then clicking the "test" url on the page would alert "somefile.htm".

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top