Yasin
Yasin

Reputation: 337

to change some css property once backing to an already-visited page

There are 2 pages, a link from first page to second one.www.example.com/second.html.

then a link from second page to a specific section on first one www.example.com/first.html#specificSection.

is there a way to change some css property on first page once backing from second one by Java Script?

Upvotes: 0

Views: 48

Answers (2)

Arman P.
Arman P.

Reputation: 4394

You can check the url on first page load, and if it is www.example.com/first.html#specificSection assume that the user came from the second page and apply any styling to your elements.

So you would check

(function () {
  if (window.location.href === 'www.example.com/first.html#specificSection') {
    // do anything here
  }
})();

UPDATE according to @Ravenous comment
You can also check document.referrer and compare it with the url of your second page. That would be more precise way to detect if the user came from the second page I think.

Upvotes: 0

Ravenous
Ravenous

Reputation: 1160

Yes.

let urlChunks = window.location.href.split('#'); 
if (urlChunks.length === 2 && urlChunks[1] === 'specificSection') {
    // add your class
}

If length is 2, it means a parameter was found after #, checking its value is safe in the condition that follows the and.

Upvotes: 1

Related Questions