1

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

所以我有这个:

var current_url = location.pathname;

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

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

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

我错过了什么吗?

谢谢!

4

1 回答 1

1

var current_url = location.pathname,

location.path names returns you the path after your host name i.e. after mysite.com in your case. But there no segment after your host name, so you will not be able to find nothing.

Please find some of the properties available in window.location given below and choose respective property required for your requirement. window.location contains

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

}

于 2014-09-24T18:03:45.033 回答