Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get part of the path with jQuery?

Tags:

jquery

I have following URL www.webiste.com/Services/allservices.html

How could I get part of this URL with jQuery?

e.g 'services' or 'allservices.html'

Any suggestion much appreciated.

like image 337
Iladarsda Avatar asked Jul 07 '11 11:07

Iladarsda


People also ask

How to get Last Part of url in jQuery?

href; part = (url. toString()). split('/') // this will give an array then choose your part by calling this array!! // Hope this will help you!!!

How do I find the URL path?

window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page.

How use contains in jQuery?

The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).

How can get current URL with parameters in jQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.


2 Answers

var url = 'http://www.website.com/Services/allservices.html';

// Getting the file name (allservices.html)
var fn = url.split('/').reverse()[0];

// Getting the extension (html)
var ext = url.split('/').reverse()[0].split('.').reverse()[0];

// Getting the last middle part (services)
var lm = url.split('/').reverse()[1];
like image 141
Simeon Avatar answered Oct 11 '22 08:10

Simeon


var url = "www.webiste.com/Services/allservices.html"
    url = url.split("/");
    alert(url[1]);

In the above example split the string url when find / (separator) and save it in an array. Then using the proper index, you can use the substring you wish (in the above example will alert Services).

Demo: http://jsfiddle.net/jcNSs/

More info for split() : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

like image 24
Sotiris Avatar answered Oct 11 '22 06:10

Sotiris