detect iframe load error

前端 未结 3 785
清酒与你
清酒与你 2021-02-07 10:13

I am loading a user-selected page into an iframe using the src property. If the load fails, I would like to report the problem in terms that will make sense to the user. iframe

3条回答
  •  时光取名叫无心
    2021-02-07 10:20

    As you've hinted, you'll face same-origin-policy type restrictions when you try to query anything inside the iframe if it's on a separate domain.

    You could make an AJAX GET request to the iframe's URL before you pass it into the src of the frame. If you don't get an HTTP 200 response back from the AJAX call, then the site won't be able to load inside the frame either.

    This will add overhead to the whole process, and is only useful if you're checking whether the iframe's document is a real URL that works. It won't help if you need to know when the iframe document has fully loaded.

    If you need to know when the iframe has loaded, and it's on an external domain, then I believe you have no other option but to ask for some code to be added to those external sites to notify the parent page that they've loaded successfully.

    Or, if it makes sense to do so, ask the end user to click a link to flag up that the content isn't loading correctly.

提交回复
热议问题