I am using iframe in react component.I want to check if the url has responded 200 and loaded completely and want to handle error and loading scenarios. I tried to access the iframe id but it's throwing corss origin frame error. Please suggest
Asked
Active
Viewed 790 times
1 Answers
0
If you want add a loading before iframe loaded completely you can handle it by checking when docuemnt of iframe loaded document.getElementById('myframe').contentWindow.document
you can create a function in your react component and check iframe like this:
const [ loading, setLoading ] = useState(true);
const checkIframe = () => {
const iframeDocument = document.getElementById('myframe').contentWindow.document;
if(iframeDocument){
setLoading(false);
} else{
setTimeout(() => {
checkIframe();
}, 1000);
}
}

Mohsen Mahoski
- 452
- 1
- 5
- 14
-
But by trying to access myframe, it gives cross orgin frame error. – user3599672 Jan 13 '22 at 07:29
-
actually this is a security issue if you access a iframe with javascript : https://stackoverflow.com/a/25098153/6606524 – Mohsen Mahoski Jan 13 '22 at 07:48