使用javascript是否可以根据用户是否来自特定的web地址(可能使用Referer)添加一些CSS?

使用javascript是否可以根据用户是否来自特定的web地址(可能使用Referer)添加一些CSS?,javascript,php,css,wordpress,Javascript,Php,Css,Wordpress,我们在我们的子域(即landing.ourcompany.com)上有一个登录页,我们将人们转发到我们的Wordpress网站页面(ourcompany.com/page),但有一个内容项(带有CSS类),如果他们来自特定链接(即landing.ourcompany.com),我们希望“显示:无”。使用javascript是否可以检测引用链接,然后将CSS添加到页面?是的,正如您所说,您可以在页面中执行以下操作 if(document.referrer == "google.com&q

我们在我们的子域(即landing.ourcompany.com)上有一个登录页,我们将人们转发到我们的Wordpress网站页面(ourcompany.com/page),但有一个内容项(带有CSS类),如果他们来自特定链接(即landing.ourcompany.com),我们希望“显示:无”。使用javascript是否可以检测引用链接,然后将CSS添加到页面?

是的,正如您所说,您可以在页面中执行以下操作

if(document.referrer == "google.com"){
    document.body.classList.add("googleRef") //or whatever you want
}
.googleRef .divThatIWantToHide{
    display: none
}
然后,由于类位于主体上,因此样式表可以有如下内容

if(document.referrer == "google.com"){
    document.body.classList.add("googleRef") //or whatever you want
}
.googleRef .divThatIWantToHide{
    display: none
}
因此body标记元素上的类作为一个标志来知道要隐藏什么。考虑到if语句需要改进