From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../client-side_storage/index.html | 780 +++++++++++++++++++++ .../javascript/client-side_web_apis/index.html | 37 + 2 files changed, 817 insertions(+) create mode 100644 files/ko/learn/javascript/client-side_web_apis/client-side_storage/index.html create mode 100644 files/ko/learn/javascript/client-side_web_apis/index.html (limited to 'files/ko/learn/javascript/client-side_web_apis') diff --git a/files/ko/learn/javascript/client-side_web_apis/client-side_storage/index.html b/files/ko/learn/javascript/client-side_web_apis/client-side_storage/index.html new file mode 100644 index 0000000000..e2ac144050 --- /dev/null +++ b/files/ko/learn/javascript/client-side_web_apis/client-side_storage/index.html @@ -0,0 +1,780 @@ +--- +title: Client-side storage +slug: Learn/JavaScript/Client-side_web_APIs/Client-side_storage +translation_of: Learn/JavaScript/Client-side_web_APIs/Client-side_storage +--- +

{{LearnSidebar}}

+ +
{{PreviousMenu("Learn/JavaScript/Client-side_web_APIs/Video_and_audio_APIs", "Learn/JavaScript/Client-side_web_APIs")}}
+ +
+ +

현대 웹 브라우저들은 (사용자의 허락 하에) 사용자 컴퓨터에 웹사이트 정보를 저장할 수 있는 다양한 방법을 제공합니다. 그리고 필요한때 그 정보들을 읽어오죠. 이는 당신이 장기간 데이터를 보관할 수 있게 해주고 사이트와 웹문서를 당신이 지정한 설정에 따라 오프라인 상태에서도 사용할수 있게 해줍니다. 이 문서는 이러한 것들이 어떻게 동작하는지에 대한 기본지식들을 설명합니다.  

+ + + + + + + + + + + + +
Prerequisites:JavaScript basics (see first steps, building blocks, JavaScript objects), the basics of Client-side APIs
Objective:To learn how to use client-side storage APIs to store application data.
+ +

Client-side storage?

+ +

우리는 다른 MDN 학습영역에서 정적인 사이트동적인 사이트에 대해 이미 설명하였습니다. 현대의 대부분의 웹사이트들은 어떤 데이터베이스(서버의 저장소)를 이용하여 서버에 데이터를 저장하고, 필요한 데이터를 찾아오기 위해 서버-사이드 코드를 돌리고, 정적인 페이지 템플릿에 데이터를 삽입하고, HTML 결과물을 사용자의 브라우저에 표시될 수 있게 제공합니다 - 즉 동적입니다.

+ +

클라이언트-사이드 저장소는 비슷한 원리로 작동하지만, 다르게 쓰입니다. 이것은 개발자가 클라이언트 측(사용자의 컴퓨터 등)에 데이터를 저장할 수 있고 필요할 때 가져올 수 있게 해주는 자바스크립트 API로 구성되어 있습니다. 이것의 다양한 용도는 다음과 같습니다.

+ + + +

클라이언트-사이드 저장소와 서버-사이드 저장소는 대개 함께 사용됩니다. 예를 들면, 당신은 (아마도 웹 게임이나 음악 재생 어플리케이션에서 사용할)음악 파일 여러 개를 다운받아 클라이언트-사이드 데이터베이스에 저장하고 필요할 때 재생할 수 있습니다. 사용자는 음악 파일을 한번만 다운받고, 재방문 시에는 데이터베이스에서 가져오기만 하면 됩니다.

+ +
+

Note: There are limits to the amount of data you can store using client-side storage APIs (possibly both per individual API and cumulatively); the exact limit varies depending on the browser and possibly based on user settings. See Browser storage limits and eviction criteria for more information.

+
+ +

Old fashioned: cookies

+ +

클라이언트-사이드 저장소에 대한 개념은 오래전부터 있었습니다. 웹의 태동기 시절, 웹 사이트들은 사용자 경험(UX)을 개인화하는 정보들을 저장하기 위해 cookies를 사용했습니다. 그것들이 웹에서 보편적으로 사용된 클라이언트-사이드 저장소의 제일 오래된 형태입니다.

+ +

오늘날에는 클라이언트 사이드에 데이터를 저장하는 더 쉬운 방법이 있지만, 이 문서에서 cookies를 사용하는 법을 가르쳐 주지는 않습니다. 그러나, 이것이 현대의 웹에서 cookies가 완벽하게 쓸모없다는 것을 뜻하지는 않습니다. cookies는 세션 ID나 access token 같은 사용자 상태와 개인화에 관련된 정보를 저장하는데 여전히 보편적으로 쓰입니다. cookies에 대한 더 자세한 정보는 우리의 Using HTTP cookies 문서를 참고하세요.

+ +

New school: Web Storage and IndexedDB

+ +

현대의 브라우저들은 클라이언트-사이드 데이터를 저장하는 데에 cookies보다 더 쉽고 더 효율적인 API들을 제공합니다.

+ + + +

밑에서 이런 API들을 더 배울 수 있습니다.

+ +

The future: Cache API

+ +

몇몇 현대적인 브라우저들은 새로운 {{domxref("Cache")}} API를 제공합니다. 이 API는 특정 requests에 대한 HTTP responses를 저장하기 위해 디자인되었고, 웹사이트가 차후에 네트워크 연결 없이도 사용될 수 있도록 사이트 정보를 저장하는 등의 일을 하는데 유용합니다. Cache는 일반적으로 Service Worker API와 함께 사용되지만, 꼭 그럴 필요는 없습니다.

+ +

Cache와 Service Workers의 사용은 심화 주제이므로 이 문서에서는 아래의 {{anch("Offline asset storage")}} 섹션에서 보여주는 것 이상으로 깊게 다루지는 않을 것입니다.

+ +

Storing simple data — web storage

+ +

The Web Storage API is very easy to use — you store simple name/value pairs of data (limited to strings, numbers, etc.) and retrieve these values when needed.

+ +

Basic syntax

+ +

Let's show you how:

+ +
    +
  1. +

    First, go to our web storage blank template on GitHub (open this in a new tab).

    +
  2. +
  3. +

    Open the JavaScript console of your browser's developer tools.

    +
  4. +
  5. +

    All of your web storage data is contained within two object-like structures inside the browser: {{domxref("Window.sessionStorage", "sessionStorage")}} and {{domxref("Window.localStorage", "localStorage")}}. The first one persists data for as long as the browser is open (the data is lost when the browser is closed) and the second one persists data even after the browser is closed and then opened again. We'll use the second one in this article as it is generally more useful.

    + +

    The {{domxref("Storage.setItem()")}} method allows you to save a data item in storage — it takes two parameters: the name of the item, and its value. Try typing this into your JavaScript console (change the value to your own name, if you wish!):

    + +
    localStorage.setItem('name','Chris');
    +
  6. +
  7. +

    The {{domxref("Storage.getItem()")}} method takes one parameter — the name of a data item you want to retrieve — and returns the item's value. Now type these lines into your JavaScript console:

    + +
    var myName = localStorage.getItem('name');
    +myName
    + +

    Upon typing in the second line, you should see that the myName variable now contains the value of the name data item.

    +
  8. +
  9. +

    The {{domxref("Storage.removeItem()")}} method takes one parameter — the name of a data item you want to remove — and removes that item out of web storage. Type the following lines into your JavaScript console:

    + +
    localStorage.removeItem('name');
    +var myName = localStorage.getItem('name');
    +myName
    + +

    The third line should now return null — the name item no longer exists in the web storage.

    +
  10. +
+ +

The data persists!

+ +

One key feature of web storage is that the data persists between page loads (and even when the browser is shut down, in the case of localStorage). Let's look at this in action.

+ +
    +
  1. +

    Open our web storage blank template again, but this time in a different browser to the one you've got this tutorial open in! This will make it easier to deal with.

    +
  2. +
  3. +

    Type these lines into the browser's JavaScript console:

    + +
    localStorage.setItem('name','Chris');
    +var myName = localStorage.getItem('name');
    +myName
    + +

    You should see the name item returned.

    +
  4. +
  5. +

    Now close down the browser and open it up again.

    +
  6. +
  7. +

    Enter the following lines again:

    + +
    var myName = localStorage.getItem('name');
    +myName
    + +

    You should see that the value is still available, even though the browser has been closed and then opened again.

    +
  8. +
+ +

Separate storage for each domain

+ +

There is a separate data store for each domain (each separate web address loaded in the browser). You will see that if you load two websites (say google.com and amazon.com) and try storing an item on one website, it won't be available to the other website.

+ +

This makes sense — you can imagine the security issues that would arise if websites could see each other's data!

+ +

A more involved example

+ +

Let's apply this new-found knowledge by writing a simple working example to give you an idea of how web storage can be used. Our example will allow you enter a name, after which the page will update to give you a personalized greeting. This state will also persist across page/browser reloads, because the name is stored in web storage.

+ +

You can find the example HTML at personal-greeting.html — this contains a simple website with a header, content, and footer, and a form for entering your name.

+ +

+ +

Let's build up the example, so you can understand how it works.

+ +
    +
  1. +

    First, make a local copy of our personal-greeting.html file in a new directory on your computer.

    +
  2. +
  3. +

    Next, note how our HTML references a JavaScript file called index.js (see line 40). We need to create this and write our JavaScript code into it. Create an index.js file in the same directory as your HTML file. 

    +
  4. +
  5. +

    We'll start off by creating references to all the HTML features we need to manipulate in this example — we'll create them all as constants, as these references do not need to change in the lifecycle of the app. Add the following lines to your JavaScript file:

    + +
    // create needed constants
    +const rememberDiv = document.querySelector('.remember');
    +const forgetDiv = document.querySelector('.forget');
    +const form = document.querySelector('form');
    +const nameInput = document.querySelector('#entername');
    +const submitBtn = document.querySelector('#submitname');
    +const forgetBtn = document.querySelector('#forgetname');
    +
    +const h1 = document.querySelector('h1');
    +const personalGreeting = document.querySelector('.personal-greeting');
    +
  6. +
  7. +

    Next up, we need to include a small event listener to stop the form from actually submitting itself when the submit button is pressed, as this is not the behavior we want. Add this snippet below your previous code:

    + +
    // Stop the form from submitting when a button is pressed
    +form.addEventListener('submit', function(e) {
    +  e.preventDefault();
    +});
    +
  8. +
  9. +

    Now we need to add an event listener, the handler function of which will run when the "Say hello" button is clicked. The comments explain in detail what each bit does, but in essence here we are taking the name the user has entered into the text input box and saving it in web storage using setItem(), then running a function called nameDisplayCheck() that will handle updating the actual website text. Add this to the bottom of your code:

    + +
    // run function when the 'Say hello' button is clicked
    +submitBtn.addEventListener('click', function() {
    +  // store the entered name in web storage
    +  localStorage.setItem('name', nameInput.value);
    +  // run nameDisplayCheck() to sort out displaying the
    +  // personalized greetings and updating the form display
    +  nameDisplayCheck();
    +});
    +
  10. +
  11. +

    At this point we also need an event handler to run a function when the "Forget" button is clicked — this is only displayed after the "Say hello" button has been clicked (the two form states toggle back and forth). In  this function we remove the name item from web storage using removeItem(), then again run nameDisplayCheck() to update the display. Add this to the bottom:

    + +
    // run function when the 'Forget' button is clicked
    +forgetBtn.addEventListener('click', function() {
    +  // Remove the stored name from web storage
    +  localStorage.removeItem('name');
    +  // run nameDisplayCheck() to sort out displaying the
    +  // generic greeting again and updating the form display
    +  nameDisplayCheck();
    +});
    +
  12. +
  13. +

    It is now time to define the nameDisplayCheck() function itself. Here we check whether the name item has been stored in web storage by using localStorage.getItem('name') as a conditional test. If it has been stored, this call will evaluate to true; if not, it will be false. If it is true, we display a personalized greeting, display the "forget" part of the form, and hide the "Say hello" part of the form. If it is false, we display a generic greeting and do the opposite. Again, put the following code at the bottom:

    + +
    // define the nameDisplayCheck() function
    +function nameDisplayCheck() {
    +  // check whether the 'name' data item is stored in web Storage
    +  if(localStorage.getItem('name')) {
    +    // If it is, display personalized greeting
    +    let name = localStorage.getItem('name');
    +    h1.textContent = 'Welcome, ' + name;
    +    personalGreeting.textContent = 'Welcome to our website, ' + name + '! We hope you have fun while you are here.';
    +    // hide the 'remember' part of the form and show the 'forget' part
    +    forgetDiv.style.display = 'block';
    +    rememberDiv.style.display = 'none';
    +  } else {
    +    // if not, display generic greeting
    +    h1.textContent = 'Welcome to our website ';
    +    personalGreeting.textContent = 'Welcome to our website. We hope you have fun while you are here.';
    +    // hide the 'forget' part of the form and show the 'remember' part
    +    forgetDiv.style.display = 'none';
    +    rememberDiv.style.display = 'block';
    +  }
    +}
    +
  14. +
  15. +

    Last but not least, we need to run the nameDisplayCheck() function every time the page is loaded. If we don't do this, then the personalized greeting will not persist across page reloads. Add the following to the bottom of your code:

    + +
    document.body.onload = nameDisplayCheck;
    +
  16. +
+ +

Your example is finished — well done! All that remains now is to save your code and test your HTML page in a browser. You can see our finished version running live here.

+ +
+

Note: There is another, slightly more complex example to explore at Using the Web Storage API.

+
+ +
+

Note: In the line <script src="index.js" defer></script> of the source for our finished version, the defer attribute specifies that the contents of the {{htmlelement("script")}} element will not execute until the page has finished loading.

+
+ +

Storing complex data — IndexedDB

+ +

The IndexedDB API (sometimes abbreviated IDB) is a complete database system available in the browser in which you can store complex related data, the types of which aren't limited to simple values like strings or numbers. You can store videos, images, and pretty much anything else in an IndexedDB instance.

+ +

However, this does come at a cost: IndexedDB is much more complex to use than the Web Storage API. In this section, we'll really only scratch the surface of what it is capable of, but we will give you enough to get started.

+ +

Working through a note storage example

+ +

Here we'll run you through an example that allows you to store notes in your browser and view and delete them whenever you like, getting you to build it up for yourself and explaining the most fundamental parts of IDB as we go along.

+ +

The app looks something like this:

+ +

+ +

Each note has a title and some body text, each individually editable. The JavaScript code we'll go through below has detailed comments to help you understand what's going on.

+ +

Getting started

+ +
    +
  1. First of all, make local copies of our index.html, style.css, and index-start.js files into a new directory on your local machine.
  2. +
  3. Have a look at the files. You'll see that the HTML is pretty simple: a web site with a header and footer, as well as a main content area that contains a place to display notes, and a form for entering new notes into the database. The CSS provides some simple styling to make it clearer what is going on. The JavaScript file contains five declared constants containing references to the {{htmlelement("ul")}} element the notes will be displayed in, the title and body {{htmlelement("input")}} elements, the {{htmlelement("form")}} itself, and the {{htmlelement("button")}}.
  4. +
  5. Rename your JavaScript file to index.js. You are now ready to start adding code to it.
  6. +
+ +

Database initial set up

+ +

Now let's look at what we have to do in the first place, to actually set up a database.

+ +
    +
  1. +

    Below the constant declarations, add the following lines:

    + +
    // Create an instance of a db object for us to store the open database in
    +let db;
    + +

    Here we are declaring a variable called db — this will later be used to store an object representing our database. We will use this in a few places, so we've declared it globally here to make things easier.

    +
  2. +
  3. +

    Next, add the following to the bottom of your code:

    + +
    window.onload = function() {
    +
    +};
    + +

    We will write all of our subsequent code inside this window.onload event handler function, called when the window's {{event("load")}} event fires, to make sure we don't try to use IndexedDB functionality before the app has completely finished loading (it could fail if we don't).

    +
  4. +
  5. +

    Inside the window.onload handler, add the following:

    + +
    // Open our database; it is created if it doesn't already exist
    +// (see onupgradeneeded below)
    +let request = window.indexedDB.open('notes_db', 1);
    + +

    This line creates a request to open version 1 of a database called notes_db. If this doesn't already exist, it will be created for you by subsequent code. You will see this request pattern used very often throughout IndexedDB. Database operations take time. You don't want to hang the browser while you wait for the results, so database operations are {{Glossary("asynchronous")}}, meaning that instead of happening immediately, they will happen at some point in the future, and you get notified when they're done.

    + +

    To handle this in IndexedDB, you create a request object (which can be called anything you like — we called it request so it is obvious what it is for). You then use event handlers to run code when the request completes, fails, etc., which you'll see in use below.

    + +
    +

    Note: The version number is important. If you want to upgrade your database (for example, by changing the table structure), you have to run your code again with an increased version number, different schema specified inside the onupgradeneeded handler (see below), etc. We won't cover upgrading databases in this simple tutorial.

    +
    +
  6. +
  7. +

    Now add the following event handlers just below your previous addition — again inside the window.onload handler:

    + +
    // onerror handler signifies that the database didn't open successfully
    +request.onerror = function() {
    +  console.log('Database failed to open');
    +};
    +
    +// onsuccess handler signifies that the database opened successfully
    +request.onsuccess = function() {
    +  console.log('Database opened successfully');
    +
    +  // Store the opened database object in the db variable. This is used a lot below
    +  db = request.result;
    +
    +  // Run the displayData() function to display the notes already in the IDB
    +  displayData();
    +};
    + +

    The {{domxref("IDBRequest.onerror", "request.onerror")}} handler will run if the system comes back saying that the request failed. This allows you to respond to this problem. In our simple example, we just print a message to the JavaScript console.

    + +

    The {{domxref("IDBRequest.onsuccess", "request.onsuccess")}} handler on the other hand will run if the request returns successfully, meaning the database was successfully opened. If this is the case, an object representing the opened database becomes available in the {{domxref("IDBRequest.result", "request.result")}} property, allowing us to manipulate the database. We store this in the db variable we created earlier for later use. We also run a custom function called displayData(), which displays the data in the database inside the {{HTMLElement("ul")}}. We run it now so that the notes already in the database are displayed as soon as the page loads. You'll see this defined later on.

    +
  8. +
  9. +

    Finally for this section, we'll add probably the most important event handler for setting up the database: {{domxref("IDBOpenDBRequest.onupgradeneeded", "request.onupdateneeded")}}. This handler runs if the database has not already been set up, or if the database is opened with a bigger version number than the existing stored database (when performing an upgrade). Add the following code, below your previous handler:

    + +
    // Setup the database tables if this has not already been done
    +request.onupgradeneeded = function(e) {
    +  // Grab a reference to the opened database
    +  let db = e.target.result;
    +
    +  // Create an objectStore to store our notes in (basically like a single table)
    +  // including a auto-incrementing key
    +  let objectStore = db.createObjectStore('notes_os', { keyPath: 'id', autoIncrement:true });
    +
    +  // Define what data items the objectStore will contain
    +  objectStore.createIndex('title', 'title', { unique: false });
    +  objectStore.createIndex('body', 'body', { unique: false });
    +
    +  console.log('Database setup complete');
    +};
    + +

    This is where we define the schema (structure) of our database; that is, the set of columns (or fields) it contains. Here we first grab a reference to the existing database from e.target.result (the event target's result property), which is the request object. This is equivalent to the line db = request.result; inside the onsuccess handler, but we need to do this separately here because the onupgradeneeded handler (if needed) will run before the onsuccess handler, meaning that the db value wouldn't be available if we didn't do this.

    + +

    We then use {{domxref("IDBDatabase.createObjectStore()")}} to create a new object store inside our opened database called notes_os. This is equivalent to a single table in a conventional database system. We've given it the name notes, and also specified an autoIncrement key field called id — in each new record this will automatically be given an incremented value — the developer doesn't need to set this explicitly. Being the key, the id field will be used to uniquely identify records, such as when deleting or displaying a record.

    + +

    We also create two other indexes (fields) using the {{domxref("IDBObjectStore.createIndex()")}} method: title (which will contain a title for each note), and body (which will contain the body text of the note).

    +
  10. +
+ +

So with this simple database schema set up, when we start adding records to the database; each one will be represented as an object along these lines:

+ +
{
+  title: "Buy milk",
+  body: "Need both cows milk and soya.",
+  id: 8
+}
+ +

Adding data to the database

+ +

Now let's look at how we can add records to the database. This will be done using the form on our page.

+ +

Below your previous event handler (but still inside the window.onload handler), add the following line, which sets up an onsubmit handler that runs a function called addData() when the form is submitted (when the submit {{htmlelement("button")}} is pressed leading to a successful form submission):

+ +
// Create an onsubmit handler so that when the form is submitted the addData() function is run
+form.onsubmit = addData;
+ +

Now let's define the addData() function. Add this below your previous line:

+ +
// Define the addData() function
+function addData(e) {
+  // prevent default - we don't want the form to submit in the conventional way
+  e.preventDefault();
+
+  // grab the values entered into the form fields and store them in an object ready for being inserted into the DB
+  let newItem = { title: titleInput.value, body: bodyInput.value };
+
+  // open a read/write db transaction, ready for adding the data
+  let transaction = db.transaction(['notes_os'], 'readwrite');
+
+  // call an object store that's already been added to the database
+  let objectStore = transaction.objectStore('notes_os');
+
+  // Make a request to add our newItem object to the object store
+  var request = objectStore.add(newItem);
+  request.onsuccess = function() {
+    // Clear the form, ready for adding the next entry
+    titleInput.value = '';
+    bodyInput.value = '';
+  };
+
+  // Report on the success of the transaction completing, when everything is done
+  transaction.oncomplete = function() {
+    console.log('Transaction completed: database modification finished.');
+
+    // update the display of data to show the newly added item, by running displayData() again.
+    displayData();
+  };
+
+  transaction.onerror = function() {
+    console.log('Transaction not opened due to error');
+  };
+}
+ +

This is quite complex; breaking it down, we:

+ + + +

Displaying the data

+ +

We've referenced displayData() twice in our code already, so we'd probably better define it. Add this to your code, below the previous function definition:

+ +
// Define the displayData() function
+function displayData() {
+  // Here we empty the contents of the list element each time the display is updated
+  // If you didn't do this, you'd get duplicates listed each time a new note is added
+  while (list.firstChild) {
+    list.removeChild(list.firstChild);
+  }
+
+  // Open our object store and then get a cursor - which iterates through all the
+  // different data items in the store
+  let objectStore = db.transaction('notes_os').objectStore('notes_os');
+  objectStore.openCursor().onsuccess = function(e) {
+    // Get a reference to the cursor
+    let cursor = e.target.result;
+
+    // If there is still another data item to iterate through, keep running this code
+    if(cursor) {
+      // Create a list item, h3, and p to put each data item inside when displaying it
+      // structure the HTML fragment, and append it inside the list
+      let listItem = document.createElement('li');
+      let h3 = document.createElement('h3');
+      let para = document.createElement('p');
+
+      listItem.appendChild(h3);
+      listItem.appendChild(para);
+      list.appendChild(listItem);
+
+      // Put the data from the cursor inside the h3 and para
+      h3.textContent = cursor.value.title;
+      para.textContent = cursor.value.body;
+
+      // Store the ID of the data item inside an attribute on the listItem, so we know
+      // which item it corresponds to. This will be useful later when we want to delete items
+      listItem.setAttribute('data-note-id', cursor.value.id);
+
+      // Create a button and place it inside each listItem
+      let deleteBtn = document.createElement('button');
+      listItem.appendChild(deleteBtn);
+      deleteBtn.textContent = 'Delete';
+
+      // Set an event handler so that when the button is clicked, the deleteItem()
+      // function is run
+      deleteBtn.onclick = deleteItem;
+
+      // Iterate to the next item in the cursor
+      cursor.continue();
+    } else {
+      // Again, if list item is empty, display a 'No notes stored' message
+      if(!list.firstChild) {
+        let listItem = document.createElement('li');
+        listItem.textContent = 'No notes stored.';
+        list.appendChild(listItem);
+      }
+      // if there are no more cursor items to iterate through, say so
+      console.log('Notes all displayed');
+    }
+  };
+}
+ +

Again, let's break this down:

+ + + +

Deleting a note

+ +

As stated above, when a note's delete button is pressed, the note is deleted. This is achieved by the deleteItem() function, which looks like so:

+ +
// Define the deleteItem() function
+function deleteItem(e) {
+  // retrieve the name of the task we want to delete. We need
+  // to convert it to a number before trying it use it with IDB; IDB key
+  // values are type-sensitive.
+  let noteId = Number(e.target.parentNode.getAttribute('data-note-id'));
+
+  // open a database transaction and delete the task, finding it using the id we retrieved above
+  let transaction = db.transaction(['notes_os'], 'readwrite');
+  let objectStore = transaction.objectStore('notes_os');
+  let request = objectStore.delete(noteId);
+
+  // report that the data item has been deleted
+  transaction.oncomplete = function() {
+    // delete the parent of the button
+    // which is the list item, so it is no longer displayed
+    e.target.parentNode.parentNode.removeChild(e.target.parentNode);
+    console.log('Note ' + noteId + ' deleted.');
+
+    // Again, if list item is empty, display a 'No notes stored' message
+    if(!list.firstChild) {
+      let listItem = document.createElement('li');
+      listItem.textContent = 'No notes stored.';
+      list.appendChild(listItem);
+    }
+  };
+}
+ + + +

So that's it! Your example should now work.

+ +

If you are having trouble with it, feel free to check it against our live example (see the source code also).

+ +

Storing complex data via IndexedDB

+ +

As we mentioned above, IndexedDB can be used to store more than just simple text strings. You can store just about anything you want, including complex objects such as video or image blobs. And it isn't much more difficult to achieve than any other type of data.

+ +

To demonstrate how to do it, we've written another example called IndexedDB video store (see it running live here also). When you first run the example, it downloads all the videos from the network, stores them in an IndexedDB database, and then displays the videos in the UI inside {{htmlelement("video")}} elements. The second time you run it, it finds the videos in the database and gets them from there instead before displaying them — this makes subsequent loads much quicker and less bandwidth-hungry.

+ +

Let's walk through the most interesting parts of the example. We won't look at it all — a lot of it is similar to the previous example, and the code is well-commented.

+ +
    +
  1. +

    For this simple example, we've stored the names of the videos to fetch in an array of objects:

    + +
    const videos = [
    +  { 'name' : 'crystal' },
    +  { 'name' : 'elf' },
    +  { 'name' : 'frog' },
    +  { 'name' : 'monster' },
    +  { 'name' : 'pig' },
    +  { 'name' : 'rabbit' }
    +];
    +
  2. +
  3. +

    To start with, once the database is successfully opened we run an init() function. This loops through the different video names, trying to load a record identified by each name from the videos database.

    + +

    If each video is found in the database (easily checked by seeing whether request.result evaluates to true — if the record is not present, it will be undefined), its video files (stored as blobs) and the video name are passed straight to the displayVideo() function to place them in the UI. If not, the video name is passed to the fetchVideoFromNetwork() function to ... you guessed it — fetch the video from the network.

    + +
    function init() {
    +  // Loop through the video names one by one
    +  for(let i = 0; i < videos.length; i++) {
    +    // Open transaction, get object store, and get() each video by name
    +    let objectStore = db.transaction('videos_os').objectStore('videos_os');
    +    let request = objectStore.get(videos[i].name);
    +    request.onsuccess = function() {
    +      // If the result exists in the database (is not undefined)
    +      if(request.result) {
    +        // Grab the videos from IDB and display them using displayVideo()
    +        console.log('taking videos from IDB');
    +        displayVideo(request.result.mp4, request.result.webm, request.result.name);
    +      } else {
    +        // Fetch the videos from the network
    +        fetchVideoFromNetwork(videos[i]);
    +      }
    +    };
    +  }
    +}
    +
  4. +
  5. +

    The following snippet is taken from inside fetchVideoFromNetwork() — here we fetch MP4 and WebM versions of the video using two separate {{domxref("fetch()", "WindowOrWorkerGlobalScope.fetch()")}} requests. We then use the {{domxref("blob()", "Body.blob()")}} method to extract each response's body as a blob, giving us an object representation of the videos that can be stored and displayed later on.

    + +

    We have a problem here though — these two requests are both asynchronous, but we only want to try to display or store the video when both promises have fulfilled. Fortunately there is a built-in method that handles such a problem — {{jsxref("Promise.all()")}}. This takes one argument — references to all the individual promises you want to check for fulfillment placed in an array — and is itself promise-based.

    + +

    When all those promises have fulfilled, the all() promise fulfills with an array containing all the individual fulfillment values. Inside the all() block, you can see that we then call the displayVideo() function like we did before to display the videos in the UI, then we also call the storeVideo() function to store those videos inside the database.

    + +
    let mp4Blob = fetch('videos/' + video.name + '.mp4').then(response =>
    +  response.blob()
    +);
    +let webmBlob = fetch('videos/' + video.name + '.webm').then(response =>
    +  response.blob()
    +);
    +
    +// Only run the next code when both promises have fulfilled
    +Promise.all([mp4Blob, webmBlob]).then(function(values) {
    +  // display the video fetched from the network with displayVideo()
    +  displayVideo(values[0], values[1], video.name);
    +  // store it in the IDB using storeVideo()
    +  storeVideo(values[0], values[1], video.name);
    +});
    +
  6. +
  7. +

    Let's look at storeVideo() first. This is very similar to the pattern you saw in the previous example for adding data to the database — we open a readwrite transaction and get a reference to our videos_os object store, create an object representing the record to add to the database, then simply add it using {{domxref("IDBObjectStore.add()")}}.

    + +
    function storeVideo(mp4Blob, webmBlob, name) {
    +  // Open transaction, get object store; make it a readwrite so we can write to the IDB
    +  let objectStore = db.transaction(['videos_os'], 'readwrite').objectStore('videos_os');
    +  // Create a record to add to the IDB
    +  let record = {
    +    mp4 : mp4Blob,
    +    webm : webmBlob,
    +    name : name
    +  }
    +
    +  // Add the record to the IDB using add()
    +  let request = objectStore.add(record);
    +
    +  ...
    +
    +};
    +
  8. +
  9. +

    Last but not least, we have displayVideo(), which creates the DOM elements needed to insert the video in the UI and then appends them to the page. The most interesting parts of this are those shown below — to actually display our video blobs in a <video> element, we need to create object URLs (internal URLs that point to the video blobs stored in memory) using the {{domxref("URL.createObjectURL()")}} method. Once that is done, we can set the object URLs to be the values of our {{htmlelement("source")}} element's src attributes, and it works fine.

    + +
    function displayVideo(mp4Blob, webmBlob, title) {
    +  // Create object URLs out of the blobs
    +  let mp4URL = URL.createObjectURL(mp4Blob);
    +  let webmURL = URL.createObjectURL(webmBlob);
    +
    +  ...
    +
    +  let video = document.createElement('video');
    +  video.controls = true;
    +  let source1 = document.createElement('source');
    +  source1.src = mp4URL;
    +  source1.type = 'video/mp4';
    +  let source2 = document.createElement('source');
    +  source2.src = webmURL;
    +  source2.type = 'video/webm';
    +
    +  ...
    +}
    +
  10. +
+ +

Offline asset storage

+ +

The above example already shows how to create an app that will store large assets in an IndexedDB database, avoiding the need to download them more than once. This is already a great improvement to the user experience, but there is still one thing missing — the main HTML, CSS, and JavaScript files still need to downloaded each time the site is accessed, meaning that it won't work when there is no network connection.

+ +

+ +

This is where Service workers and the closely-related Cache API come in.

+ +

A service worker is a JavaScript file that, simply put, is registered against a particular origin (web site, or part of a web site at a certain domain) when it is accessed by a browser. When registered, it can control pages available at that origin. It does this by sitting between a loaded page and the network and intercepting network requests aimed at that origin.

+ +

When it intercepts a request, it can do anything you wish to it (see use case ideas), but the classic example is saving the network responses offline and then providing those in response to a request instead of the responses from the network. In effect, it allows you to make a web site work completely offline.

+ +

The Cache API is a another client-side storage mechanism, with a bit of a difference — it is designed to save HTTP responses, and so works very well with service workers.

+ +
+

Note: Service workers and Cache are supported in most modern browsers now. At the time of writing, Safari was still busy implementing it, but it should be there soon.

+
+ +

A service worker example

+ +

Let's look at an example, to give you a bit of an idea of what this might look like. We have created another version of the video store example we saw in the previous section — this functions identically, except that it also saves the HTML, CSS, and JavaScript in the Cache API via a service worker, allowing the example to run offline!

+ +

See IndexedDB video store with service worker running live, and also see the source code.

+ +

Registering the service worker

+ +

The first thing to note is that there's an extra bit of code placed in the main JavaScript file (see index.js). First we do a feature detection test to see if the serviceWorker member is available in the {{domxref("Navigator")}} object. If this returns true, then we know that at least the basics of service workers are supported. Inside here we use the {{domxref("ServiceWorkerContainer.register()")}} method to register a service worker contained in the sw.js file against the origin it resides at, so it can control pages in the same directory as it, or subdirectories. When its promise fulfills, the service worker is deemed registered.

+ +
  // Register service worker to control making site work offline
+
+  if('serviceWorker' in navigator) {
+    navigator.serviceWorker
+             .register('/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/sw.js')
+             .then(function() { console.log('Service Worker Registered'); });
+  }
+ +
+

Note: The given path to the sw.js file is relative to the site origin, not the JavaScript file that contains the code. The service worker is at https://mdn.github.io/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/sw.js. The origin is https://mdn.github.io, and therefore the given path has to be /learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/sw.js. If you wanted to host this example on your own server, you'd have to change this accordingly. This is rather confusing, but it has to work this way for security reasons.

+
+ +

Installing the service worker

+ +

The next time any page under the service worker's control is accessed (e.g. when the example is reloaded), the service worker is installed against that page, meaning that it will start controlling it. When this occurs, an install event is fired against the service worker; you can write code inside the service worker itself that will respond to the installation.

+ +

Let's look at an example, in the sw.js file (the service worker). You'll see that the install listener is registered against self. This self keyword is a way to refer to the global scope of the service worker from inside the service worker file.

+ +

Inside the install handler we use the {{domxref("ExtendableEvent.waitUntil()")}} method, available on the event object, to signal that the browser shouldn't complete installation of the service worker until after the promise inside it has fulfilled successfully.

+ +

Here is where we see the Cache API in action. We use the {{domxref("CacheStorage.open()")}} method to open a new cache object in which responses can be stored (similar to an IndexedDB object store). This promise fulfills with a {{domxref("Cache")}} object representing the video-store cache. We then use the {{domxref("Cache.addAll()")}} method to fetch a series of assets and add their responses to the cache.

+ +
self.addEventListener('install', function(e) {
+ e.waitUntil(
+   caches.open('video-store').then(function(cache) {
+     return cache.addAll([
+       '/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/',
+       '/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/index.html',
+       '/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/index.js',
+       '/learning-area/javascript/apis/client-side-storage/cache-sw/video-store-offline/style.css'
+     ]);
+   })
+ );
+});
+ +

That's it for now, installation done.

+ +

Responding to further requests

+ +

With the service worker registered and installed against our HTML page, and the relevant assets all added to our cache, we are nearly ready to go. There is only one more thing to do, write some code to respond to further network requests.

+ +

This is what the second bit of code in sw.js does. We add another listener to the service worker global scope, which runs the handler function when the fetch event is raised. This happens whenever the browser makes a request for an asset in the directory the service worker is registered against.

+ +

Inside the handler we first log the URL of the requested asset. We then provide a custom response to the request, using the {{domxref("FetchEvent.respondWith()")}} method.

+ +

Inside this block we use {{domxref("CacheStorage.match()")}} to check whether a matching request (i.e. matches the URL) can be found in any cache. This promise fulfills with the matching response if a match is not found, or undefined if it isn't.

+ +

If a match is found, we simply return it as the custom response. If not, we fetch() the response from the network and return that instead.

+ +
self.addEventListener('fetch', function(e) {
+  console.log(e.request.url);
+  e.respondWith(
+    caches.match(e.request).then(function(response) {
+      return response || fetch(e.request);
+    })
+  );
+});
+ +

And that is it for our simple service worker. There is a whole load more you can do with them — for a lot more detail, see the service worker cookbook. And thanks to Paul Kinlan for his article Adding a Service Worker and Offline into your Web App, which inspired this simple example.

+ +

Testing the example offline

+ +

To test our service worker example, you'll need to load it a couple of times to make sure it is installed. Once this is done, you can:

+ + + +

If you refresh your example page again, you should still see it load just fine. Everything is stored offline — the page assets in a cache, and the videos in an IndexedDB database.

+ +

Summary

+ +

That's it for now. We hope you've found our rundown of client-side storage technologies useful.

+ +

See also

+ + + +

{{PreviousMenu("Learn/JavaScript/Client-side_web_APIs/Video_and_audio_APIs", "Learn/JavaScript/Client-side_web_APIs")}}

+ +

In this module

+ + diff --git a/files/ko/learn/javascript/client-side_web_apis/index.html b/files/ko/learn/javascript/client-side_web_apis/index.html new file mode 100644 index 0000000000..a02e2bc0c4 --- /dev/null +++ b/files/ko/learn/javascript/client-side_web_apis/index.html @@ -0,0 +1,37 @@ +--- +title: Client-side web APIs +slug: Learn/JavaScript/Client-side_web_APIs +translation_of: Learn/JavaScript/Client-side_web_APIs +--- +
{{LearnSidebar}}
+ +

웹 사이트 또는 응용 프로그램 용 client-side JavaScript를 작성할때 API (응용 프로그램 프로그래밍 인터페이스)를 사용하지 않으면 많은것을 만들기 어렵습니다. API는 사이트가 실행되고 있는 브라우저와 운영 체제의 다양한 부분 그리고 웹 사이트 혹은 서비스의 데이터를 다룰 수 있는 인터페이스입니다. 이번장에서 우리는 API가 무엇인지, 그리고 개발할 때 자주 접하게 될 보편적인 API를 사용하는 방법에 대해 살펴볼 것입니다.

+ +

선행사항

+ +

다음의 문서를 미리 보고 오시는것을 권장합니다. (First steps, Building blocks, and JavaScript objects). 이 모듈은 클라이언트 측 JavaScript 예제를 사용하지 않고 유용하게 사용할 수 있기 때문에 간단한 API 사용법을 상당히 많이 사용합니다. 여기에서는 핵심 자바 스크립트 언어에 대한 지식이 있다고 가정하고 일반적인 웹 API를 좀 더 자세하게 살펴보면서 한 단계 올라갑니다. 이번장에서는 심플한 API사용방법을 여럿 보도록 하겠습니다. 이 API들은 client-side 자바스크립트를 만드는데 도움이 많이 될 것입니다.

+ +

HTMCSS에 관한 기본지식이 있으면 좋습니다!

+ +
+

Note: 코드를 작성 할 수 없는 디바이스에서 작업하는 경우 JSBin 또는 Thimble.과 같은 온라인 코딩 프로그램에서 코드 예제를 시험해 볼 수 있습니다.

+
+ +

Guides

+ +
+
Introduction to web APIs
+
우선, 높은 수준의 API를 먼저 살펴볼 것입니다. API는 무엇이며, 어떻게 작동하며, 코드에서 어떻게 사용하고 구조화되어 있는지 확인합니다. 또한 API의 다른 주요 클래스가 무엇인지, 그리고 용도가 무엇인지 살펴볼 것입니다.
+
Manipulating documents
+
웹 페이지와 앱을 작성할 때 많이 하는 것중 하나는 웹 문서를 다루는 것입니다. 보통 {{domxref ( "Document")}} 객체를 많이 사용하는 HTML 및 스타일 정보를 제어하기위한 API 집합 인 DOM (Document Object Model)을 사용합니다. 여기서는 흥미로운 방식으로 환경을 변경할 수 있는 API와 함께 DOM을 사용하는 방법에 대해 자세히 설명합니다.
+
Fetching data from the server
+
최신 웹 사이트 및 응용 프로그램에서 필요한 작업중 하나는 완전히 새로운 페이지를 로드 할 필요없이 서버에서 개별 데이터를 가져와 웹 페이지의 일부분만 업데이트하는 것입니다. 이렇게 하면 사이트의 성능과 동작에 큰 영향을 줍니다. 여기서는 이 개념을 설명하고 {{domxref ( "XMLHttpRequest")}}나 Fetch API와 같은 기술을 살펴 보겠습니다
+
Third party APIs
+
지금까지 살펴본 API는 브라우저에 내장되어 있지만 모든 API가 지원되지는 않습니다. 예를들면 Google Maps, Twitter, Facebook, PayPal 등과 같은 많은 대형 웹 사이트와 서비스가 있고 여기에는 개발자 자신의 데이터 (예 : 블로그에 트위터 스트림 표시) 또는 서비스 (예 : 사이트에 맞춤 Google지도 표시, Facebook 로그인을 사용하여 사용자 로그인)가 있습니다.여기서는 브라우저 API와 타사 API의 차이점을 살펴본 후 일반적인 API 사용법을 보겠습니다.
+
Drawing graphics
+
브라우저에는 SVG (Scalable Vector Graphics) 언어부터 HTML {{htmlelement ( "canvas")}} 요소 (The Canvas API 및 WebGL 참조)를 그리는 데 필요한 API에 이르기까지 매우 강력한 그래픽 프로그래밍 도구가 포함되어 있습니다. 여기서는 Canvas API에 대한 소개와 더 많은 정보를 얻을 수있는 추가 자료를 제공합니다.
+
Video and audio APIs
+
HTML5에는 문서 ({{htmlelement ( "video")}} 및 {{htmlelement ( "audio")}}에 미디어를 포함하기위한 요소가 포함되어 있으며 재생, 탐색 등을 제어하는 자체 API가 제공됩니다. 이문서에서는 사용자 커스텀 재생 컨트롤 만들기와 같은 일반적인 작업을 수행하는 방법을 보여줍니다.
+
Client-side storage
+
최신 웹 브라우저에는 웹 사이트와 관련된 데이터를 저장하고 필요할 때 검색하여 장기간 데이터를 유지하고 오프라인으로 사이트를 저장하는 등 다양한 기술을 사용할 수 있습니다. 이 기사에서는 이러한 작업 방식에 대한 기본적인 내용을 설명합니다.
+
-- cgit v1.2.3-54-g00ecf