如果当前URL是Home Do Something

时间:2014-09-24 17:43:01

标签: javascript jquery url if-statement pathname

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

所以我有这个:

var current_url = location.pathname;

在网站的主页(即mysite.com)上,current_url记录为'/'。

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

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

我错过了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

var current_url = location.pathname,

location.path names返回主机名后的路径,即在您的情况下在mysite.com之后。但是在您的主机名之后没有任何段,所以您将无法找到任何内容。

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

         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:",

}

相关问题