最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check the content after the "" on a URL with jQuery - Stack Overflow

programmeradmin2浏览0评论

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 or window.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
Add a ment  | 

2 Answers 2

Reset to default 8

Check 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.
}
发布评论

评论列表(0)

  1. 暂无评论