recherche

Maison  >  Questions et réponses  >  le corps du texte

Comment masquer tous les IFrames dans une page HTML si la source IFrame contient des erreurs XML ?

J'essaye iframe 标记内显示 PDF 预览时遇到问题。我有一个页面包含一些 iframes 以在页面中显示 PDF 文件,问题是有时源 URL 中不存在 PDF,因此 iframe 显示 XML 错误,指出 BlobNotFound。如果在 iframe 的源代码中返回该错误,我想隐藏 iframe.

J'ai essayé le script JavaScript suivant :

<script>
  const myIframe = document.getElementById('myFrame1');
  fetch('example.xml')
      .then(response => {
          if (!response.ok) {
              throw new Error('XML file not found');
          }
          return response.text();
      })
      .then(xmlString => {
          const parser = new DOMParser();
          const xmlDoc = parser.parseFromString(xmlString, "text/xml");
          // check for errors in the XML content
          const error = xmlDoc.getElementsByTagName("parsererror");
          if (error.length > 0) {
              myIframe.style.display = 'none'; // hide the iframe if there is an error
          }
      })
      .catch(error => {
          console.error(error);
          myIframe.style.display = 'none'; // hide the iframe if there is an error
      });
</script>

Ce code fonctionne, mais seulement si vous le spécifiez via ID iframe 标记,并且我想重写此代码以一次检查所有 iframe.

J'ai également essayé le code suivant :

<script>
  const iframes = document.getElementsByTagName('iframe');
  for (let i = 0; i < iframes.length; i++) {
      const iframe = iframes[i];
      fetch(iframe.src)
          .then(response => {
              if (!response.ok) {
                  throw new Error('XML file not found');
              }
              return response.text();
          })
          .then(xmlString => {
              const parser = new DOMParser();
              const xmlDoc = parser.parseFromString(xmlString, "text/xml");
              // check for errors in the XML content
              const error = xmlDoc.getElementsByTagName("parsererror");
              if (error.length > 0) {
                  iframe.style.display = 'none'; // hide the iframe if there is an error
              }
          })
          .catch(error => {
              console.error(error);
              iframe.style.display = 'none'; // hide the iframe if there is an error
          });
  }
</script>

Ce code devrait fonctionner, mais d'une manière ou d'une autre, il cache tous les iframe même s'il ne contient aucune erreur.

P.S. Il s'agit d'une erreur XML pour tout iframe qui ne peut pas afficher le PDF.

<?xml version="1.0" encoding="utf-8"?>
<Error>
    <Code>BlobNotFound</Code>
    <Message>
        The specified blob does not exist.
            RequestId:xxxxxxxx
            Time:timestamp
    </Message>
</Error>

P粉308089080P粉308089080414 Il y a quelques jours493

répondre à tous(1)je répondrai

  • P粉043295337

    P粉0432953372024-01-11 12:50:34

    Vous pouvez essayer ça !

    <!DOCTYPE html>
    <html>
    <head>
      <title>Hide Iframes with XML Error</title>
    </head>
    <body>
      <iframe src="path/to/pdf1.pdf"></iframe>
      <iframe src="path/to/pdf2.pdf"></iframe>
      <iframe src="path/to/nonexistent.pdf"></iframe>
      <iframe src="path/to/pdf3.pdf"></iframe>
      <script>
        var iframes = document.getElementsByTagName('iframe');
        for (var i = 0; i < iframes.length; i++) {
          var iframe = iframes[i];
          iframe.contentWindow.addEventListener('error', function(event) {
            if (event.message === 'BlobNotFound') {
              // Hide all iframes if any one of them has an XML error
              for (var j = 0; j < iframes.length; j++) {
                iframes[j].style.display = 'none';
              }
            }
          });
        }
      </script>
    </body>
    </html>

    S'il vous plaît, dites-moi si cela fonctionne.

    répondre
    0
  • Annulerrépondre