Stopping a iframe from loading a page using javascript

前端 未结 6 542
情话喂你
情话喂你 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条回答
  •  借酒劲吻你
    2020-12-01 13:14

    Very easy:

    1) Get the iframe or img you don't want to load:

    let myIframe = document.getElementById('my-iframe')
    

    2) Then you can just replace src attribute to about.blank:

    myIframe.src = 'about:blank'
    

    That's all.


    If you wanted to load the iframe or image at a time in feature when some event happens then just store the src variable in dataset:

    myIframe.dataset.srcBackup = myIframe.src
    // then replace by about blank
    myIframe.src = 'about:blank'
    

    Now you can use it when needed easily:

    myIframe.src = myIframe.dataset.srcBackup
    

提交回复
热议问题