HTML Tag

Definition and Usage

The <script> tag is used to define a client-side script (JavaScript).

The <script> element either contains scripting statements, or it points to an external script file through the src attribute.

Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Tip: If you want to learn JavaScript, visit our JavaScript
Tutorial
.


Attributes

= New in HTML5.

Attribute Value Description
async async Specifies that the script is executed asynchronously (only for external scripts)
charset charset Specifies the character encoding used in an external script file
defer defer Specifies that the script is executed when the page has finished parsing (only for external scripts)
src URL Specifies the URL of an external script file
type media_type Specifies the media type of the script
xml:space preserve Not supported in HTML5.
Specifies whether whitespace in code should be preserved

script defer async

  • Normal execution <script>
    This is the default behavior of the <script> element. Parsing of the HTML code pauses while the script is executing. For slow servers and heavy scripts this means that displaying the webpage will be delayed.
  • Deferred execution <script defer>
    Simply put: delaying script execution until the HTML parser has finished. A positive effect of this attribute is that the DOM will be available for your script. However, since not every browser supports defer yet, don’t rely on it!
  • Asynchronous execution <script async>
    Don’t care when the script will be available? Asynchronous is the best of both worlds: HTML parsing may continue and the script will be executed as soon as it’s ready. I’d recommend this for scripts such as Google Analytics.

 

我:特性是指同時做很多事嗎?
W:async = 發出要求~收到結果 這中間我可以去做其他事情
W:你填好單子送到櫃檯,然後……
W:他辦完再叫你的號碼→Async
W:要站在櫃檯等他辦完→Sync
W:因為不用站在櫃檯等,你就可以跑很多櫃檯、送很多張單子出去, 
   就有上面說的「圍毆」的效果。
W:所以 sync/async 是在說單一櫃檯辦事員的運作模式。
W:然後……
W:在對方是 async 的前提下,有些事情還是要依序辦理,
   例如:先去開好戶頭,才把戶頭交給公司行政。
W:那你腦內就要有個清單:
W:等薪資戶開好,然後再去跟公司行政帳號。
W:這個然後就是 promise 的 then
W:再歸納一次:
W:- sync/async 是指單一個要求or操作的運作方式。
    (你需不需要卡住等他做完)
W:- promise 則是用來描述多個操作之間關係的方法。
    (可以應付 async,但也可以是 sync)
W:以上

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *