title: How to get the directory part of current URL in JavaScript?
body: <p>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.</p>
  
  <p>For example, clicking that button while viewing the URL</p>
  
  <p><a href="http://example.com/somedir/button.html">http://example.com/somedir/button.html</a></p>
  
  <p>would redirect you to the </p>
  
  <p><a href="http://example.com/somedir/">http://example.com/somedir/</a></p>
  
  <p>So I've created the following code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;input type="button"
  value="back to dir"
  onclick="top.location=document.URL.replace(/[^\\]*$/, '')"&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>but I'm missing the correct code, which would shave away the filename from the current URL in <code>document.URL</code></p>
  
  <p>Does anybody have a good idea here please?</p>
  
  <p>Here is the JSFiddle link: <a href="http://jsfiddle.net/afarber/PERBY/">http://jsfiddle.net/afarber/PERBY/</a></p>
  
  <p>And I'd prefer not to use jQuery this one time.</p>