How to use JavaScript to redirect a page if a specific div class does not exist?
P粉821274260
2023-08-15 16:31:21
<p>I am trying to create a redirect if a specific div class does not exist in the HTML code of the page<strong>. </strong></p>
<p> So far I've written the following code, but I guess now it's the "opposite way" (redirect if the div exists, not if it doesn't): </p>
<pre class="brush:php;toolbar:false;">if (!document.getElementsByClassName("flickity-lazyloaded")[0]) {
window.location.href = "/404";}</pre>
<p>How do I make it work the way I want? </p>
<p>FYI: The weird thing is that when I create a JSFiddle it works as expected - but on an actual website it redirects me despite the presence of the ".flickity-lazyloaded" class in the HTML: < ;/p>
<p>https://jsfiddle.net/v97k5new/1/</p>