PDA

View Full Version : removing everything after the final slash in a URL


ush
04-29-2003, 06:23 PM
Hello!

[1]
In my custom 404 page (which can't use PHP, CFM, ASP, etc.) I'd like to give the user a chance to visit 'one level up'. I can't do this with a link to "../" as the 404 page has a BASE HREF="http://www.thegrid.org.uk/" tag in the HEAD of the document so that the images, css, etc, will always work, no matter what the current URL of the missing document is...

So what I need to do is strip off everything after the final "/" (using reg exps?) and offer that as a "try this link and see if it works!" link on my page....

So:

if the URL is http://www.thegrid.org.uk/foo/bar/foo.html

I'd like to offer the user http://www.thegrid.org.uk/foo/bar/ as a link to try.

[2]
Ideally, if the address was http://www.thegrid.org.uk/foo/bar/foo.html I'd like to offer them a choice of
http://www.thegrid.org.uk/foo/bar/
http://www.thegrid.org.uk/foo/
http://www.thegrid.org.uk/

so that they can *definitely* find a page that works, but that's out of my range using JS - isn't it?

Any tips welcome and greatly appreciated!

ian.

beetle
04-29-2003, 06:56 PM
Do you want these choices in a dropdown list? (a SELECT element?)

liorean
04-29-2003, 07:05 PM
var
re=/^(http:\/\/[^\/])\/?(.*)$/,
aHref=(location.href).match(re);
aHref=[aHref[1]].concat(aHref[2].split(/\//g));
// Now, the contents of aHref will be an array like this:
// [0]: domain (eg "http://example.net")
// [1...(n-1)]: directory
// [n]: file (and hash (eg "#hash") and search (eg "?search") string)

// Up one directory:
aHref.pop();
sPath=aHref.join('/');
// Top directory
sPath=aHref.shift();

// For those in between, aHref.pop() removes one each time used.

beetle
04-29-2003, 07:21 PM
<html>
<head>
<title>test</title>

<script type="text/javascript">

document.createHTMLElement = function( elemName, attribs )
{
var elem = document.createElement( elemName );
if ( typeof attribs != 'undefined' )
{
for ( var i in attribs )
{
switch ( true )
{
case ( i == 'text' ) : elem.appendChild( document.createTextNode( attribs[i] ) ); break;
case ( i == 'class' ) : elem.className = attribs[i]; break;
default : elem.setAttribute( i, '' ); elem[i] = attribs[i];
}
}
}
return elem;
}

function makeLinks( parentId )
{
var url = "http://www.thegrid.org.uk/foo/bar/foo.html";
var html = document.createHTMLElement;
var s = html( 'select' );

var o = html( 'option', {value:'',text:'Choose a link'} );
s.appendChild( o );

while( /http:\/\/[^/]+\/.+/.test( url ) )
{
url = url.replace( /\/[^/]+\/?$/, "/" );
o = html( 'option', {text:url,value:url} );
s.appendChild( o );
}
s.onchange = function()
{
var i = this.selectedIndex;
if ( i != 0 )
{
top.location.href = this.options[i].value
}
}
document.getElementById( parentId ).appendChild( s );
}

</script>

</head>

<body onload="makeLinks('blah')">

<div id="blah"></div>

</body>
</html>

ush
04-30-2003, 07:12 PM
The bit below is still true for IE - but not for Netscape 6 or 7 :(

Apologies, but I'm neither a CF Nut or a CF Addict, so you'll have to speak slowly!

Liorean - how would I incorporate your answer into a page - is it simply a case of document.write -ing the variables?

Peter, is your method tweakable to work in NS?

original rushed reply was:
fantastic, thanks folks, I've used beetle's answer - see it at

http://www.thegrid.org.uk/about/content/maps.htm

(which is a duff link)

cheers!

ian.