Eduardo Go
Eduardo Go

Reputation: 401

jQuery/JS: Get current URL parent directory

From:

http://www.site.com/example/index.html

How can I get just:

http://www.site.com/example/

And storing it into a variable using Javascript and how using jQuery also. Thanks in advance.

Upvotes: 10

Views: 11982

Answers (6)

dmail
dmail

Reputation: 33

The following seems to work

new URL(".", "http://example.com/folder/subfolder/file.js")

Upvotes: 2

jqgsninimo
jqgsninimo

Reputation: 7038

$(location).prop("href").split("/").slice(0,-1).join("/")

Demo process with the current page:

  1. $(location)

    {
        "ancestorOrigins": {
        },
        "hash": "",
        "host": "stackoverflow.com",
        "hostname": "stackoverflow.com",
        "href": "https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory",
        "origin": "https://stackoverflow.com",
        "pathname": "/questions/17497045/jquery-js-get-current-url-parent-directory",
        "port": "",
        "protocol": "https:",
        "search": ""
    }
    
  2. $(location).prop("href")

    https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory
    
  3. $(location).prop("href").split("/")

    [
        "https:",
        "",
        "stackoverflow.com",
        "questions",
        "17497045",
        "jquery-js-get-current-url-parent-directory"
    ]
    
  4. $(location).prop("href").split("/").slice(0,-1)

    [
        "https:",
        "",
        "stackoverflow.com",
        "questions",
        "17497045"
    ]
    

    ※ The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. Use negative numbers to select from the end of an array.

  5. $(location).prop("href").split("/").slice(0,-1).join("/")

    https://stackoverflow.com/questions/17497045
    

Notes and References:

  • location: The location object contains information about the current URL.
  • href: The entire URL of the current page.
  • .prop(): Get the value of a property for the element.
  • .split(): The method is used to split a string into an array of substrings, and returns the new array.
  • .slice(): The method returns the selected elements in an array, as a new array object.
  • .join(): The method joins the elements of an array into a string, and returns the string.

Upvotes: 5

fr4nk
fr4nk

Reputation: 176

http://jsfiddle.net/mXpBx/

var s1 = "http://www.site.com/example/index.html";
var s2 = s1.replace(s1.split("/").pop(),"");

Upvotes: 4

bjo
bjo

Reputation: 550

A regex would do the same, but in this example a regex is not the most simple solution.

var url = "http://www.site.com/example/index.html";
var newUrl = url.match(/^(.*[\\\/])/)[1];

Upvotes: 1

go-oleg
go-oleg

Reputation: 19480

var myURL = "http://www.site.com/example/index.html";
var myDir = myURL.substring( 0, myURL.lastIndexOf( "/" ) + 1);

Upvotes: 12

Sergio
Sergio

Reputation: 28837

Fiddle

var a = "http://www.site.com/example/index.html";
var b = a.substring(0, a.lastIndexOf('/'))+"/";

Upvotes: 1

Related Questions