PDA

Click to See Complete Forum and Search --> : Vb -> JS


Rh0ads
Oct 8th, 2002, 04:22 PM
Hi, I'm fairily new to Javascript and not too good in it. I want to make a script that prints the name of the file on the page. ex: it's located at http://www.blah.com/dir/page.html and i want the page to say "page". I have this code from VB, which does the trick:

URL=Replace(URL,".html","")
URL=Replace(URL,".htm","")
For a& = Len(URL) To 1 Step -1
If Mid(URL, a, 1) = "/" Then
b$ = Mid(URL, a + 1, Len(URL) - a)
MsgBox b
End
End If
Next a

Can anyone give me the Javascript equivilant? or something that accomplishes what i described above?

Thanks
-George

JoshT
Oct 8th, 2002, 07:15 PM
<script type="text/javascript">
re = /^.*\/(.*)\.[a-zA-Z]+$/;
result = re.exec(document.location);
alert(result[1]);
</script>

is one way of doing it using a regular expression.

JoshT
Oct 8th, 2002, 07:18 PM
And if you want to win an obfusicated code contest, you can do that in one line:

alert(/^.*\/(.*)\.[a-zA-Z]+$/.exec(document.location)[1]);