所以我有一个 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.path 名称会返回您的主机名之后的路径,即您的情况下 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:",
}
关于javascript - 如果当前 URL 是主页,请执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023213/