Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: open

Message Too Old, No Replies

Javascript string manipulation

Finding the name of the current page

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

10+ Year Member



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:

unescape(window.location.pathname)

to give me the complete path but instead of:

[domain.com...]

my code needs just:

filename.htm.

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)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

10+ Year Member



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)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

10+ Year Member



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
}
}
//-->
</SCRIPT>

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

10+ Year Member



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