PDA

View Full Version : Grabbing URL html file name


florida
09-09-2004, 08:20 PM
I have an entry where someone enters a URL. I need to do a replace on that form entry and just fetch the html file name.

So if someone enters: http://myurlpath/myfile.html
I want to forward just myfile.html to the action page.

Here is my attempt:

<script>
function grabURLPagename()
{
document.mypagename.myurlfield.value=document.mypagename.myurlfield.replace(/?????/, '/\*.html/');
}
</script>

<form name=mypagename action=Myaction.cfm onsubmit="return grabURLPagename()";>
...
</form>


Please advise because I am not sure how to do the reg expression or if I am doing this correctly?

Willy Duitt
09-09-2004, 08:28 PM
It may be best to use *.substring(*lastIndexOf('/'), *value.length)....

florida
09-09-2004, 10:30 PM
Thanks,

I would do it like this?


document.mypagename.myurlfield.value=document.mypagename.myurlfield.substring(lastIndexOf('/'), value.length)

glenngv
09-10-2004, 06:53 AM
You can also use the substr() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194618) method.

function grabURLPagename()
{
var str = document.mypagename.myurlfield.value;
document.mypagename.myurlfield.value = str.substr(str.lastIndexOf('/'));
return true;
}

You can simplify the function if you pass the form object to it.

function grabURLPagename(oForm)
{
var str = oForm.myurlfield.value;
oForm.myurlfield.value = str.substr(str.lastIndexOf('/'));
return true;
}
...
<form name="mypagename" action="Myaction.cfm" onsubmit="return grabURLPagename(this);">

florida
09-10-2004, 01:22 PM
Thanks!