Stopping a iframe from loading a page using javascript

前端 未结 6 540
情话喂你
情话喂你 2020-12-01 12:36

Is there a way in javascript of stopping an iframe in the middle of loading a page? The reason I need to do this is I have a background iframe streaming data from a web serv

6条回答
  •  萌比男神i
    2020-12-01 13:00

    If you only have a reference to the element, you need to use .contentX to get the document/window to run the accepted answer.
    Checking that the iframe actually has a document is also necessary for dynamically added iframe elements.

    function stopIframe(element) {
        var doc = element.contentDocument;
        //iframes wont have a document if they aren't loading/loaded
        //check so JS wont throw
        if (!doc)
            return;
    
        //try for modern browsers
        if (doc.defaultView.stop)
            doc.defaultView.stop();
        //fallback for IE
        else
            doc.execCommand('Stop');
    }
    

提交回复
热议问题