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.
href; part = (url. toString()). split('/') // this will give an array then choose your part by calling this array!! // Hope this will help you!!!
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.
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).
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.
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];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With