Skip to content

Instantly share code, notes, and snippets.

@mgng
Created August 22, 2012 06:32
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 mgng/3422946 to your computer and use it in GitHub Desktop.
Save mgng/3422946 to your computer and use it in GitHub Desktop.
javascriptでテキストエリアの文字列をダウンロードさせる
<p>
<textarea id="id_textarea">あいうえおインータネット</textarea><br />
<button onclick="kuso()">ダウンロード</button>
<div id="id_download"></div>
</p>
<script type="text/javascript">
function kuso(){ if(window.File){
var b = new Blob(
[ document.getElementById('id_textarea').value ],
{ "type" : "text/plain" }
);
var c = window.URL
? function(b){ return window.URL.createObjectURL(b); }
: window.webkitURL
? function(b){ return window.webkitURL.createObjectURL(b); }
: undefined;
if ( c ) {
document.getElementById("id_download").innerHTML = '<a href="' + c(b) + '" target="_blank">ダウンロード</a>';
}
}}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment