Script Tag - async & defer

前端 未结 8 1665
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 13:52

I have a couple of questions about the attributes async & defer for the

8条回答
  •  迷失自我
    2020-11-22 14:20

    Faced same kind of problem and now clearly understood how both will works.Hope this reference link will be helpful...

    Async

    When you add the async attribute to your script tag, the fol­low­ing will happen.

    
    
    
    1. Make par­al­lel requests to fetch the files.
    2. Con­tinue pars­ing the doc­u­ment as if it was never interrupted.
    3. Exe­cute the indi­vid­ual scripts the moment the files are downloaded.

    Defer

    Defer is very sim­i­lar to async with one major dif­fer­er­ence. Here’s what hap­pens when a browser encoun­ters a script with the defer attribute.

    
    
    
    1. Make par­al­lel requests to fetch the indi­vid­ual files.
    2. Con­tinue pars­ing the doc­u­ment as if it was never interrupted.
    3. Fin­ish pars­ing the doc­u­ment even if the script files have downloaded.
    4. Exe­cute each script in the order they were encoun­tered in the document.

    Reference :Difference between Async and Defer

提交回复
热议问题