Retrieves the correct position of the IFrame inserted into the DOM structure

/ / create the iframe and insert the body const scheme = 'gactravel: / / target/action' const iframe = document. The createElement method (" iframe "); iframe.style.width = '1px'; iframe.style.height = '1px'; iframe.style.display = 'none'; iframe.src = scheme; document.body.appendChild(iframe); Recycling iframe / / / / the first step removes the iframe DOM Tree document. The body. The removeChild (iframe); // // or iframe.remove(); DOM iframe = null; / / release the iframeCopy the code

The wrong position

H5 running in app uses Scheme to communicate with APP. H5 creates an IFrame and initiates a Scheme in iframe, which is captured and responded to by APP.

  const scheme = 'gactravel://target/action'
  const iframe = document.createElement('iframe');
  iframe.style.width = '1px';
  iframe.style.height = '1px';
  iframe.style.display = 'none';
  iframe.src = scheme;
  document.body.appendChild(iframe);
  setTimeout(iframe.remove, 100);
Copy the code

why

Remove method in setTimeout, this in the node. remove method points to an error, directly put the method in setTimeout, remove method this will be window, correct execution needs to point to iframe.

If you want to use setTimeout to perform iframe.remove, use this

setTimeout(() = > {iframe.remove()}, 100)
// or
setTimeout(iframe.remove.bind(iframe), 100)
Copy the code