본문 바로가기

html | javascript

html javascript - 코드로 html 파일 만들기

let htmlStr = '<!DOCTYPE html>\n' +
        '<html lang="en">\n' +
        ' <head>\n' +
        '   <meta charset="UTF-8">\n' +
        '   <title>title-title-title</title>\n' +
        ' </head>\n' +
        ' <body>\n' +
        '   <label>label-label-label</label>\n' +
        ' </body>\n'+
        '</html>'

htmlStr = htmlStr.replace('title-title-title', 'save html')
htmlStr = htmlStr.replace('label-label-label', 'saved')
saveHtml(htmlStr, 'save-html-file')


function saveHtml(str, name) {
  const link = document.createElement('a')
  const file = new Blob([str], {type:'text/html'})
  link.href = URL.createObjectURL(file)
  link.download = `${name}.html`
  link.click()
  URL.revokeObjectURL(link.href)
}