how can i check if value in if Statement is an iframe

92 Views Asked by At

i tried it several times and different ways. I want to check if the first value of an if statement is an iframe. Can anyone explain to me what im doing wrong here? im really confused. I need to check if the value is an iframe or not so i can use some css to hide the div if the value isn't i tried several different variations but i hadn't find the right Syntax to check if im right. Currently the value is always an iframe but the next step would be integrate or deliver 1x1 pixel. Thats why i had to check it

const containerToProof = useRef()
useEffect(()=>{
   // console.log('saysomething',containerToProof.current.firstElementChild.innerHTML)   
var isiframe = containerToProof.current.firstElementChild.innerHTML;
console.log('isiframe', isiframe);
    if(isiframe.tagName=== iframe){
        console.log('allo')
        setAdSpotvisible(true)

    }else{

        setAdSpotvisible(false)
    }

 },[<iframe/>]);
/*
if(containerToProof.current=='iframe'){
       console.log('allo')
}*/
    return (
        <div>
           <div  ref={containerToProof}  ></div>



    );`````

1

There are 1 best solutions below

0
atbrakhi On

You can use regex for the check, atleast it worked in my case.

You can do the following:

Here is a working example:

const value = "<iframe></iframe>";
const iframeCheck = /<iframe(?: [^>]*?)?(?:\/>|>.*?<\/iframe>)/i;

if (iframeCheck.test(value)) {
  alert('It is an iframe')
} else {
  alert('It is not an iframe')
}