0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自分自身のHTMLをダウンロードさせる方法

Last updated at Posted at 2021-03-27

このようにするのだ

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Myself</title>
</head>
<body>
    <div contenteditable="true" style="width: 500px; height: 300px; border: solid 1px black;">
    ほげほげ
    </div>
    <button onclick="download()">このページをダウンロード</button>
    <script>

    function download() {
        // DOCTYPE宣言とhtmlタグとその内部のコードの文字列から自分自身のHTMLのコードの文字列を生成
        const htmlText = "<!DOCTYPE html>" + document.getElementsByTagName("html")[0].outerHTML;
        // HTMLのコードの文字列をBlobにし、aタグに紐づけてダウンロードさせる。
        const blob = new Blob([htmlText], {type:"text/plan"});
        const link = document.createElement("a");
        link.href = URL.createObjectURL(blob);
        link.download = "index.html";
        link.click();
        URL.revokeObjectURL(link.href);
    }
    
    </script>
</body>
</html>

なおIEでは動きません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?