Lets say my URL is:
/12434/example-post
I want to check if the current URL contains something after the "/"
, in other words, I wan't to check whether or not I'm at the homepage
Is this possible with jQuery?
Lets say my URL is:
http://example./12434/example-post
I want to check if the current URL contains something after the "/"
, in other words, I wan't to check whether or not I'm at the homepage http://example.
Is this possible with jQuery?
Share Improve this question edited Feb 28, 2012 at 14:38 Alicja Kario 23k4 gold badges28 silver badges45 bronze badges asked Feb 19, 2012 at 14:47 BelohlavekBelohlavek 1673 silver badges13 bronze badges 5- Not 'with jQuery' as such, but definitely with plain JavaScript (jQuery, so far as I know doesn't make it any easier or offer methods to achieve this). – David Thomas Commented Feb 19, 2012 at 14:51
- You do not need jQuery for this, raw Javascript will do. – Tadeck Commented Feb 19, 2012 at 14:51
-
@Tadeck: You mean you don't do this?
$($(window).prop('location')).prop('pathname')
:D – user1106925 Commented Feb 19, 2012 at 14:55 -
2
@amnotiam: Excellent example, a lot easier than
location.pathname
orwindow.location.pathname
, and everything is a lot faster in jQuery than in JavaScript. I am not sure why JavaScript is still being used when you have jQuery ;) – Tadeck Commented Feb 19, 2012 at 15:00 - 2 @Tadeck: Exactyl. All teh best codez are Jquery!!!!! :D – user1106925 Commented Feb 19, 2012 at 15:04
2 Answers
Reset to default 8Check location
global object. It has pathname
property:
alert( location.pathname );
window.location.href gives the url of page. Test the code
alert(window.location.href);
Does the URL contains anything afer abc./xyz i.e. xyz here can be tested with below code;
var url= window.location.href;
if(url.split("/").length>3){
// It is not the home page. It is not xyz.. It has something after /
}
else{
// you are on the home page.
}