Categorized | others

Javascript Get the Current Path without %20 and / backslash




TumblrStumbleUponRedditLinkedInAIMDiggEvernoteWhatsAppMySpaceKindle It

To get the current path using javascript without the %20 and / (backslash), use the following code:

1
2
var path = document.location.pathname;
alert (path);

However using the javascript path variable in this manner gives something like this:

javascript-current-path-with-20

So now we use the string replace function in Javascript to rectify this:

1
2
3
4
5
6
var path = document.location.pathname;
path = path.replace(/\//gi, "");
path = path.replace(/%20/gi, " ");
path = path.replace(/\\/gi, "\/");
alert (path);
var path = document.location.pathname;

This javascript code gives us the path variable without the %20 and without the / (forward slash).

javascript-get-current-path-without-20

We can now use this path to access any other file on the system relative to the above path.

How this works:
1. The path = path.replace(/\//gi, “”) instruction strips the first forward slash (/) in the path string.
2. The path = path.replace(/%20/gi, ” “) replaces any occurrence of %20 in the path string with a space as would appear on the address bar of the computer.
3. Finally we replace any backslashes with forward slashes using path = path.replace(/\\/gi, “\/”)

The syntax of the replace function in javascript is:

var my_string = any_string.replace(/old_sub_string_to_replace/gi, “new_sub_string_to_replace_with“)

Now lets use this technique and replace “script” in “javascript” with ” and C++” and see what happens.

1
2
3
var any_string= "javascript";
var my_string = any_string.replace(/script/gi, " and C++");
alert (my_string);

would give

example-of-current-path-using-javascript

The operators “g” used at the end of the string to be replaced indicates that any occurrence (global) of that specific substring needs to be replaced. The operator “i” tells the function to make a case insensitive replacement.

In case you wanted to get only the path and not the filename, you could use:

1
2
3
4
5
6
7
var path = document.location.pathname;
path = path.substr(0,path.lastIndexOf("\\")+1);
path = path.replace(/\//gi, "");
path = path.replace(/%20/gi, " ");
path = path.replace(/\\/gi, "\/");
alert (path);
var path = document.location.pathname;

This will remove the name of the file from the path.

TumblrStumbleUponRedditLinkedInAIMDiggEvernoteWhatsAppMySpaceKindle It

Excel Formula, Excel Chart, Excel Macro, Excel VBA, Pivot Table Excel, Excel Dashboard

What Do You Think ?


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Comments and Trackbacks

  1. Kars wrote:

    Hello there. Thanks for sharing. I was looking for something like this. Using href.location and a match or substring on it is so much more work. The document.location.pathname was all I needed. I wonder though, if you have to replace spaces in a pathname, I think there is already something wrong with the way you name your folders, don’t you think so?

  2. adfasdfasf wrote:

    var path = document.location.pathname;
    alert (path);

Subscribe

Keep up with the latest stories - Delivered right to your inbox
feedburner

Translate

English flagChinese (Simplified) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flagArabic flagDutch flagHindi flagPolish flagSwedish flagFilipino flagHebrew flagIndonesian flagUkrainian flagThai flagTurkish flag
treeemap software for excel