Images created using `document.currentScript.ownerDocument.createElement` (from within <link> imported HTML) never load

前端 未结 1 1889
天命终不由人
天命终不由人 2020-12-21 19:09

Here\'s a function that returns the width of an image, given the image\'s url:

async function getImageWidthByUrl(imgUrl) {
  const imgEl = document.createEle         


        
相关标签:
1条回答
  • 2020-12-21 19:28

    It's the normal behaviour.

    The image referenced in the src attribute <img> element in an imported document with <link rel="import"> is not loaded until it is appended to the main document.

    From the HTML Imports tutorial:

    Think of content as inert until you call upon its services. [...]

    Unless you append it's content to the DOM, it's a no-op. In fact, the only thing that "executes" in the import document directly is <script>.

    You can apend it to the main document with document.apendNode().

    Also, note that in your example, you should pass the imported document reference as an argument of the main function of simple-test.html for some reasons explained in this post or that post.

    ( function( ownerDocument ) {
        //...
        if( method === "document.currentScript.ownerDocument.createElement") {
            imgEl = ownerDocument.createElement("img")
            var imgOK = document.adoptNode( imgEl )
        }
        //...  
    } ) ( document.currentScript.ownerDocument )
    
    0 讨论(0)
提交回复
热议问题