TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • The “script” tag
  • Modern markup
  • External scripts

Was this helpful?

  1. JavaScript

Hello, world!

JavaScript 可以在很多不同環境裡執行 ( server、browser )。

PreviousAn Introduction to JavaScriptNextCode structure

Last updated 5 years ago

Was this helpful?

The “script” tag

使用 <script> JavaScript 可以被放在任何 HTML 裡面。

Modern markup

很舊的 <script> code

  • <script type='...'> 在 HTML 4 需要這樣寫,HTML 5 不需要這樣的寫法,HTML 5 完全改變 type 屬性的意思,現在可以被用來 。

  • <script language='...'> 用來表示 <script> 的語言,現在不需要這樣寫了。

  • <script type='...'> <!-- ... --> </script> 在 <script> 中間寫註解,現在不會這樣使用了。

External scripts

從外部引入JavaScript 的方法

<script src="/path/to/script.js"></script> //絕對路徑
<script src="script.js"></script> //相對路徑
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js"></script> //url

如果有很多 JavaScript 的 code,分開成不同的檔案放進去,這樣的好處是 browser 只會下載一次並儲存到快取,其他頁面要用到同樣的檔案只要到快取拿,而不用再下載一次。

<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
…

JavaScript Modules