Last active
August 28, 2017 07:53
-
-
Save khanhtdbse/50a0cee1f11bc81bf41e1de459ef5d17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Đăng ký service worker nếu trình duyệt hỗ trợ | |
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', () => { | |
navigator.serviceWorker.register('/sw.js').then((registration) => { | |
console.log('ServiceWorker registration successful with scope: ', registration.scope); | |
}, function (err) { | |
console.log('ServiceWorker registration failed: ', err); | |
}); | |
}); | |
} | |
// API URL | |
const serverURL = 'http://localhost:8888/server.php?id=' + articleID | |
// Lấy dữ liệu từ API | |
fetch(serverURL).then(res => res.json()).then(article => setArticle(article)) | |
// Render bài viết | |
function setArticle(article) { | |
document.title = article.title | |
const rootElement = document.getElementById('app') | |
const title = document.createElement('h1') | |
title.appendChild(document.createTextNode(article.title)) | |
const id = document.createElement('h1') | |
id.appendChild(document.createTextNode(article.id)) | |
const content = document.createElement('h2') | |
content.appendChild(document.createTextNode(article.content)) | |
rootElement.appendChild(title) | |
rootElement.appendChild(id) | |
rootElement.appendChild(content) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment