Benben
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [FE102] 前端必備:JavaScript ###### Date : 2021 May. 24 - 25 #### JavaScript 與瀏覽器的溝通 - Node.js 與 瀏覽器是不一樣的 ( Runtime 不一樣 ) - DOM ( Document Object Moduel ) > 將 Document 轉成 Object - 選到想要的元素 - getElementsByTagName - getElementsByClassName - getElementById ( 只會有一個 ) - querySelecter ( 同 CSS 選擇器,但只會回傳選到第一個 ) - queryAllSelecter ( 同 CSS 選擇器,回傳全選很像陣列的陣列 ) - 改變 CSS - element.style.background = 'red' - element.style['padding-top'] = '10px' ( object like ) - element.style.paddingTop = '10px' ( JS camelCase ) - 改變 Class - element.classList.add('active') - element.classList.remove('active') - element.classList.toggle('active') ( 開 / 關 ) - 改變內容 - element.innerText = 'hello' - element.innerHTML ( 內部所以的內容包含 html 標籤 ) - element.outterHTML ( 包含本身的 html 標籤,少用 ) - 刪除、插入元素 - element.removeChild(docment.querySelector('a')) - 插入元素,如下 : ```javascript // 插入 div const element = document.querySelector('#block') const item = document.cteateElement('div') item.innerText = '123' element.appendChild(item) // 插入存文字 const element = document.querySelector('#block') const item = document.cteateTextNode('hello') element.appendChild(item) ``` #### JavaScript 網頁事件處理 - eventListener - element.addEventListener('click', onClick) - callback function - 當有事件觸發的時候,呼叫 function - event(e) - element.addEventListener('click', function(e.target)) ```javascript const element = doxument.querySelector('#block') element.addEventListener('click', onClick) function onClick(e) { console.log(e) } ``` - keydown 事件 : ```javascript const element = document.querySelector('input') element.addEventListener('keydown', function(e){ console.log(e.key) }) ``` - onSubmit & preventDefault() - 送出 form 表單,預設 methed 是 get ```javascript const element = document.querySelector('.login-form') element.addEventListener('submit', function(e){ const input1 = document.querySelector('input[name=password1]') const input2 = document.querySelector('input[name=password2]') if (input1.value !== input2.value) { alert('密碼不同') e.preventDefault() // 可以預防瀏覽器預設動作,ex. 送出動作、超連結 } }) ``` - 事件傳遞機制 : 捕獲 & 冒泡 - addEventListener 第三個參數 ( true / false ) ```javascript function addEvnet(classNmae) { document.querySelector(className) .addEvnetListener('click', function() { console.log(className, '冒泡') }, false) document.querySelector(className) .addEventListener('click', function() { console.log(className, '捕獲') }, true) } ``` - 延伸 : [DOM 的事件傳遞機制:捕獲與冒泡](https://blog.techbridge.cc/2017/07/15/javascript-event-propagation/) - 別向上級回報 : stopPropagation - 在**捕獲**接段停止傳送 : ```javascript window.addEventListener('click', function(e) { e.stopPropagation() }, true) // 放在所有的 function 前面就可以停止傳送所有事件 ``` - 停止**冒泡** : ```javascript document.querySelector('.btn') .addEventListener('click', function(e) { // e.stopPropagation() e.stopImmediatePropagation() // 立即停止其他 eventListener,下面就不會觸發 console.log('.btn click1') }) document.querySelector('.btn') .addEventListener('click', function(e) { e.stopPropagation() // 停止冒泡,還是會觸發 console.log console.log('.btn click1') }) ``` - 新手 100% 會搞錯得問題 - 常犯的錯誤 : ```javascript const elements = document.querySelectorAll('.btn') for (var i = 0; i < elements.length; i++) { element[i].addEventListener('click', function() { alert(i + 1) }) } // 不管點哪個按鈕都是 alert 最後一個數字 ``` - 可以在 button 加入 `data-value='Num'` 屬性 : ```javascript const elements = document.querySelectorAll('.btn') for (var i = 0; i < elements.length; i++) { element[i].addEventListener('click', function() { alert(e.target.getAttribute('data-value')) }) } // e.target.getAttribute 取得 attr ``` - 事件代理 : Event Delegation - 動態新增按鈕 : ```javascript let num = 3 // body 上面原本 btn 的數量 document.querySelector('.add-btn').addEventListener('click', function() { const btn = document.createElement('button') btn.setAttribute('data-btn', num) btn.classList.add('btn') btn.innerText = num num++ document.querySelector('.outer').appendChild(btn) }) document.querySelector('.outer').addEventListener('click', function(e) { // 如果有 btn 屬性才觸發點擊事件 if (e.target.classList.contains('btn')) { alert(e.target.getAttribute('data-value')) } }) ``` - 綜合範例 : 簡易密碼產生器 ```javascript document.querySelector('.btn-generate').addEventListener('click', function() { const elements = querySelectorAll('input[type=checkbox]') for (let i = 0; i < elements.length; i++) { if (elements.checked) { availableChar += elements[i].getAttrbute('data-char') } } let result = '' for (let i = 0; i < 10; i++) { const number = Math.floor(Math.random() * availableChar.length) result += availableChar[number] } document.querySelector('.result').innerText = reslut }) ``` - 綜合範例 : 通訊錄管理 ```javascript document.querySelector('.add-btn').addEventListener('click', function() { const div = document.createElement('div') div.classList.add('row') div.innerHTML = ` 姓名 : <input name='name' /> 電話 : <input name='number /> <button class='delete'>刪除<button />'` document.querySelector('.contacts').appendChild(div) }) document.querySelector('.contacts').addEventListener('click', function(e) { if (e.target.classList.contains('delete')) { // 移除 .contacts 下面的 .row document.querySelector('.contacts').removeChild( e.target.closest('.row') ) } }) ``` #### 如何在瀏覽器上儲存資料 - Cookie ( 古老 ) - 小型文字檔會自動帶到 sever - Chrome 開發者工具 > application > Storage > Cookie - Local Storage ( 推薦 ) - 可以被看作為改進的 Cookie,提供更大的儲存容量 - Chrome 開發者工具 > application > Storage > Local Storage ```javascript const oldValue = window.localStorage.getItem('text') document.querySelector('.text').value = oldValue document.querySelector('button').addEventListener('click', function() { const value = document.querySelector('.text').value windwo.localStorage.setItem('text', value) }) ``` - Sessoin Storage ( 一閃即逝,新開 tabe 就不見啦 ) - 基本上跟 Local Storage 一樣,只差存儲的時間 ```javascript const oldValue = window.seccionStorage.getItem('text') document.querySelector('.text').value = oldValue document.querySelector('button').addEventListener('click', function() { const value = document.querySelector('.text').value windwo.seccionStorage.setItem('text', value) }) ``` #### 網頁與伺服器溝通 - node.js 與 瀏覽器 發 request 的差異是甚麼 - 在瀏覽器上會被限制 - 第一種傳送方式 : form - 我要帶上某個資訊到頁面 - 第二種傳送方式 : ajax - AJAX ( Asynchronnous JavaScript And XML ) ```javascript const request = new XMLHttpRequest() request.onload = function() { if (request.status >= 200 && request.status < 400) { console.log(request.responseText) } else { console.log('err') } } request.onerror = function() { console.log('error') } // 最後一個 true/false 為是否非同步 request.open('GET', 'https://reqres.in/api/users', true) request.send() ``` - XMLHttpRequest ```javascript const request = new XMLHttpRequest() // 使用 addEventListener 監聽 load 事件 request.addEventListener('load', function() { if (request.status >= 200 && request.status < 400) { console.log(request.responseText) } else { console.log('err', request.status) } }) request.onerror = function() { console.log('error') } // 最後一個 true/false 為是否非同步 request.open('GET', 'https://reqres.in/api/users', true) request.send() ``` - Same Origin Policy ( 同源政策 ) & CORS ( Cross-Origin Resource Sharing 跨來源資源共用 ) - 相同 domain 就是同源,但 http & https 不同源 - 如果 header 沒有加 `access-control-allow-origin: *` 就絕對無法 CORS 存取 - 瀏覽器的限制 - 同源政策等,都是瀏覽器幫你加的,node.js 則沒有此限制 - 第三種傳送方式 : JSONP ( JSON with Padding ) - `<img src="url" />`, `<script src="url"></script>`不受同源政策影響,因為比較沒有安全上的疑慮 - 所以使用 `<script src="url"></script>` 來拿資料,用 function 的方式傳送資料,不常用 - 單向傳送資料 - 例如 : 如何知道有人打開你的信 ```javascript // 放一張很小或是透明的圖片,如果有人打開就會發 request 到 img 裡的 src <img width="1" height="1" src="https://example.com/user_open/123" ``` - 綜合範例 : 抓取資料並顯示 ```javascript <style> .profile { border: 1px solid #333; margin-top: 20px; display: inline-flex; align-items: center; } .profile__name { margin: 0px 10px; } </style> <body> <div class='app'> <div class='profile'> <div class='profile__name'> George Bluth</div> <img class='profile__img' src="" /> </div> </div> <body> <script> const request = new XMLHttpRequest() const container = document.querySelector('.app') request.addEventListener('load', function() { if (request.status >= 200 && request.status < 400) { const response = request.responseText const json = JSON.parse(response) const users = json.data for (let i = 0; i < user.length; i++) { const div = document.createElement('div') div.classList.add('profile') div.innetHTML = ` <div class='profile__name'> ${user[i].first_name} ${user[i].last_name}</div> <img class='profile__img' src="${user[i].avatar}" />` container.appendChild(div) } } else { console.log('err', request.status) } }) request.onerror = function() { console.log('error') } request.open('GET', 'https://reqres.in/api/users', true) request.send() </script> ``` - Client Side Rendering - 使用 JavaScript 動態 render 出來的,檢查原始碼會看不到東西 #### Ref. - [DOM 的事件傳遞機制:捕獲與冒泡](https://blog.techbridge.cc/2017/07/15/javascript-event-propagation/) - [網頁儲存 (wiki)](https://zh.wikipedia.org/zh-tw/%E7%BD%91%E9%A1%B5%E5%AD%98%E5%82%A8) - [Window.localStorage (MDN)](https://developer.mozilla.org/zh-TW/docs/Web/API/Window/localStorage) - [輕鬆理解 Ajax 與跨來源請求](https://blog.techbridge.cc/2017/05/20/api-ajax-cors-and-jsonp/) - [同源政策 (Same-origin policy) (MDN)](https://developer.mozilla.org/zh-TW/docs/Web/Security/Same-origin_policy) - [跨來源資源共用(CORS) (MDN)](https://developer.mozilla.org/zh-TW/docs/Web/HTTP/CORS) --- #### 心得 2021 May. 25 這門課我覺得老師的架構整理得很好 : 1. 5 種選元素的方法、修改 CSS、修改 Class、修改 text/html、新增/刪除元素 2. 各種事件處理、preventDefault、還有捕獲與冒泡 看到中場總結的時候,老師說其實**會這些就可以做很多事了,只差一點想像力**,聽到這覺得 : 哇,感覺我跟網路世界又更進一步了 如果你好奇到底可以做什麼或是想練習,推薦資源 : [JavaScript 30](https://javascript30.com/) ( 使用純 JavaScript 30 打造 30 件東西 ),學到這應該有能力聽得懂了 :)) 3. 資料交換的部分,form、ajax、jsonp 也是三種都有講到 特別是 jsonp ,我是第一次聽到,覺得能想出來的人真的是奇葩XDD 最後總結,老師說 JavaScript 只要熟悉下面三個東西: **UI、事件、資料**,就可以做出所有你想的到的東西了,上完覺得 : 哇,我跟網路的世界只剩一步了,就是寫好 code 後按下 run --- 以下新增 : - 2021/05/31 html 跳脫 function ```javascript function escapeHtml(unsafe) { return unsafe .replace(/&/g, '&amp;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/"/g, '&quot;') .replace(/'/g, '&#039;'); } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully