How to use JavaScript to redirect a page if a specific div class does not exist?
P粉821274260
P粉821274260 2023-08-15 16:31:21
0
1
514
<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>
P粉821274260
P粉821274260

reply all(1)
P粉613735289


if (!document.getElementsByClassName("flickity-lazyloaded")[0]) {
window.location.href = "https://stackoverflow.com"}
<div> hello </div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template