Skip to content

Instantly share code, notes, and snippets.

@khanhtdbse
Last active August 28, 2017 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanhtdbse/50a0cee1f11bc81bf41e1de459ef5d17 to your computer and use it in GitHub Desktop.
Save khanhtdbse/50a0cee1f11bc81bf41e1de459ef5d17 to your computer and use it in GitHub Desktop.
// Đă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