如果当前URL是主页,请采取措施

If Current URL is Home Do Something

本文关键字:采取措施 主页 URL 如果      更新时间:2023-09-26

所以我有一个JS脚本(使用Jquery运行),我想根据当前页面做各种事情。

所以我有这个:

var current_url = location.pathname;

在网站的主页(即mysite.com)上,current_url的日志为"/"。

但出于某种原因,我无法得到这个if语句来评估…

if (current_url == '/') {
    $('header').append(autoflowside_content);
    console.log('Home!');
}

我是不是错过了什么??

谢谢!

var current_url=location.pathname,

location.path names会返回主机名之后的路径,即mysite.com之后的路径。但是在你的主机名后面没有分段,所以你什么都找不到。

请在下面给出的window.location中找到一些可用的属性,并根据您的要求选择相应的属性。window.位置包含

         origin: "http://stackoverflow.com", 
         hash: "",
         host: "stackoverflow.com",
         hostname: "stackoverflow.com",
         href: "http://stackoverflow.com/questions/26023213/if-current-url-is-something",
         origin: "http://stackoverflow.com"
         pathname: "/questions/26023213/if-current-url-is-home-do-something",
         port: "",
         protocol: "http:",

}