homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Javascript string manipulation
Finding the name of the current page

 12:19 pm on Feb 16, 2001 (gmt 0)

A bit of Javascript I'm writing requires the filename of the page it will be executed on. Is there a simple way to get just the filename?

I can use:


to give me the complete path but instead of:


my code needs just:


I've looked for a similar command to return the filename, but so far been unsuccessful.

I really hope that there is a simpler way to do this than searching for slashes and chopping up strings etc.



 5:06 pm on Feb 16, 2001 (gmt 0)

I've run into this one, too. Always thought there should be a simple method to return just the filename, but like you, I've never found it and have resorted to working with the full pathname.

Anyone else had better luck?


 6:55 am on Feb 19, 2001 (gmt 0)

Hi! you all there,

for as the path's are concerned you can try this out,

if the path is like for eg:xyc/abc/filename.htm

so it can be ../../filename.htm


 7:28 am on Feb 19, 2001 (gmt 0)

Welcome to the forums at Webmasterworld, bchiffon.

The way I understand the question, rpking is looking for a snippet of javascript code that he can run on any page and have it automatically return the filename of the page for whatever page is currently running it.

It's true, as you said, that the path can be written in several ways. But I'm not clear how that helps find the filename his code needs.


 11:25 am on Feb 19, 2001 (gmt 0)

Try this

<SCRIPT LANGUAGE=javascript>
var tr = window.location.pathname
len = tr.length
rs = 0
for (i = len; i > 0; i--) {
vb = tr.substring(i,i+1)
if (vb == "/" && rs == 0) {
document.write('<hr>' + tr.substring(i+1,len))
rs = 1


 11:46 am on Feb 19, 2001 (gmt 0)

Nice one. Strange that it's so hard just to get the filename.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved