检查“/"在URL上使用jQuery

Check the content after the "/" on a URL with jQuery

本文关键字:URL jQuery quot 检查      更新时间:2023-09-26

假设我的URL是:

http://example.com/12434/示例帖子

我想检查当前URL是否包含"/"之后的内容,换句话说,我不想检查我是否在主页上http://example.com

jQuery可以做到这一点吗?

检查location全局对象。它具有pathname属性:

alert( location.pathname );

window.location.href提供页面的url。测试代码

alert(window.location.href);

URL是否包含abc.com/xyz之后的任何内容,即这里的xyz可以用以下代码进行测试;

var url= window.location.href;
if(url.split("/").length>3){
  // It is not the home page. It is not xyz.com. It has something after /
} 
else{
  // you are on the home page.
}