Forum Moderators: open

Message Too Old, No Replies

Can I get part of an URL with JS?

only the last part typed in

         

wintercornuk

11:06 am on Oct 25, 2004 (gmt 0)

10+ Year Member



I only want to be able to get the last section of an URL like:

[example.com...]

I want to get the 12345678 and pass it to another script.

Can this be done in javascript or do I need something like ASP?

The site's in ASP, btw.

Thanks.

adni18

12:26 pm on Oct 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

<script language=javascript>
<!--
function tellFilename() {
montage=window.location.href.split("/");
simple=montage.length-1;
alert(montage[simple])
}
//-->
</script>
<body onLoad=tellFilename()>

Lance

1:51 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



Or this:

<script type="text/javascript">  
function showPage() {
thisPage = location.href.substring((location.href.lastIndexOf("/"))+1)
alert(thisPage);
}
</script>

<body onLoad="showPage();">

wintercornuk

2:14 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



adni18,

yours works nicely. it brings up a box with the current page on it. how would i then pass that to the following code:

<%
URL="http://www.EXAMPLE.com/"
fullurl="search.asp?searchbox3=NUMBERGOESHERE
response.redirect URL & fullurl
%>

I'm trying to have a directory where someone could type WWW.EXAMPLE.COM/ISBN/123333333 and then get redirected to the right page, without having to go through the site navigation. (Where 123333333 is the ISBN number)

The other code didn't work with my cart - not much does I'm afraid...

adni18

8:33 pm on Oct 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you want to get everything before the filename too?

If so, try this:


<script language=javascript>
<!--
function tellDir() {
montage=window.location.href.split("/");
simple=montage.length-2;
final="";
for(var i=0;i<=simple;i++)
{
final=final+montage[i]+"/";
}
alert(final)
}
//-->
</script>
<body onLoad=tellDir()>

BlobFisk

11:11 am on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These threads may help also:

Getting a part of a string in JS [webmasterworld.com]

Inserting the filename by JavaScript [webmasterworld.com]

HTH