Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  67] [ 5]  / answers: 1 / hits: 23662  / 11 Years ago, fri, june 7, 2013, 12:00:00

I'm trying to add a back to dir button at the top of a web page, which would redirect the user to the same URL, but with no filename in it.



For example, clicking that button while viewing the URL



http://example.com/somedir/button.html



would redirect you to the



http://example.com/somedir/



So I've created the following code:



<html>
<body>

<input type=button
value=back to dir
onclick=top.location=document.URL.replace(/[^\]*$/, '')>

</body>
</html>


but I'm missing the correct code, which would shave away the filename from the current URL in document.URL



Does anybody have a good idea here please?



Here is the JSFiddle link: http://jsfiddle.net/afarber/PERBY/



And I'd prefer not to use jQuery this one time.


More From » redirect

 Answers
6

Try this document.URL.substr(0,document.URL.lastIndexOf('/'))



It will work for sure!


[#77756] Thursday, June 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eastonb

Total Points: 498
Total Questions: 91
Total Answers: 102

Location: Tanzania
Member since Wed, Feb 24, 2021
3 Years ago
;