问题描述
我要删除我正在构建的网站的登录页面上的导航菜单。但是,我不知道如何只定位该页面。我在其他页面上执行此操作的方式是,当我要定位到特定页面时,我写(window.location.pathname.includes(“ / pathname”))。
但是,如果我要定位网站的来源(www.hostname.com),那么似乎不能在不删除所有其他页面上的导航菜单的情况下做到这一点,因为它们都共享相同的主机名。
这是我要执行的功能:
function landingPage() {
if (window.location.origin === "www.something.com") {
document.querySelector(".main-navigation").remove("#primary-menu");
document.querySelector(".site-branding").remove(".custom-logo");
}
}
解决方法
如果要定位网站上的特定页面,则需要像其他页面一样检查location.href
或location.pathname
。
来源定义了整个网站,而不是其主页。
,function landingPage() {
if (window.location.hostname === "www.something.com" && window.location.pathname==="/") {
/*Your Condition Comes here */
}
}
window.location.hostname将返回您网站的主机名。您可以比较主机名并编写要添加的必需条件。
要了解更多信息,请点击window location
,如果将window.location.origin
与window.location.pathname
有条件地结合起来怎么办?
window.location.pathname
应该返回"/"
,如果您位于登录页面而非子页面上。