-4

is it possible to redirect to URL in certain div

like

<div id="id45">
<! -- redirect to url if reached to this div content -->
</div>
keti stam
  • 21
  • 3

1 Answers1

0

You can do it for example this way:

function myFunction() {
  if (window.innerHeight + window.scrollY >= document.getElementById("id45").offsetTop) {
      window.open("https://www.example.com");
      window.removeEventListener("scroll", myFunction);
    }
}

window.addEventListener("scroll", myFunction);
<div style="width: 100%; height: 110vw; background-color: lightBlue;"></div>

<div id="id45" style="width: 100%; height: 100px; background-color: red;">
<! -- redirect to url if reached to this div content -->
</div>

For me it works but not in this Stackoverflow´s snippet. It gets height of full browsers window, but if you copy it to your website, I hope it will work.

Note: my browser blocked this window from opening, but it is normal, as said here.

MetropolisCZ
  • 567
  • 1
  • 8
  • 20