Using javascript is it possible to add some CSS based on if the user comes from a specific web address maybe using Referrer?

Viewed 34

We have a landing page on our sub-domain (i.e. landing.ourcompany.com) that we forward people to our Wordpress website page (ourcompany.com/page) but have one content item (with a CSS class) we want to "display: none;" if they come from that specific link (i.e. landing.ourcompany.com). Is it possible with javascript to detect the Referrer link and then add the CSS to the page?

1 Answers

Yes, you can, as you said, in your page you can do something like

if(document.referrer == "google.com"){
    document.body.classList.add("googleRef") //or whatever you want
}

then, since the class in on the body, your stylesheet can have something like

.googleRef .divThatIWantToHide{
    display: none
}

So the class on the body tag element works as a flag to know what to hide. Take in consideration that the if statement needs to be improved

Related