Connecting Tech Pros Worldwide Forums | Help | Site Map

using window.location.pathname

Newbie
 
Join Date: Mar 2008
Posts: 1
#1: Mar 10 '08
I was trying to capture the pathname only when using

var from= window.location.pathname;

and I want to lose the filename. Is there another way for me to get just the path?

rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#2: Mar 10 '08

re: using window.location.pathname


Expand|Select|Wrap|Line Numbers
  1. function Path() {
  2.     var tob = {href:window.location.href.toString()};
  3.     tob.domain = document.domain;
  4.     var lr = tob.href.toString().split(/[\/\\]/g);
  5.     var tqs = window.location.search;
  6.     tob.qs = tqs ? tqs : "";
  7.     tob.file = lr.last();
  8.     tob.path = lr.slice(0, -1).join("/") + "/";
  9.     tob['char'] = document.characterSet;
  10.     tob.ext = tob.file.match(/(?:\.)[\d\w]{0,4}/)[0];
  11.     return tob;
  12. }
  13. alert(Path().path)
  14.  
  15.  
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#3: Mar 11 '08

re: using window.location.pathname


or-
var url=location.href.replace(/\\/g,'/');
alert(url.substring(0,url.lastIndexOf('/')))

(You only need the replace for local files on windows)
Reply