Skip to content

Instantly share code, notes, and snippets.

@rosterloh
Created October 27, 2014 07:46
Show Gist options
  • Save rosterloh/df5db0da1d5362bfd8da to your computer and use it in GitHub Desktop.
Save rosterloh/df5db0da1d5362bfd8da to your computer and use it in GitHub Desktop.
Javascript - Creating a downloadable file
<!DOCTYPE html>
<html>
<head>
<style type="text/css">...</style>
<script type="text/JavaScript">
window.onload = function() {
var txt = document.getElementById('txt');
txt.value = window.onload + '';
document.getElementById('link').onclick = function(code) {
this.href = 'data:text/plain;charset=utf-8,'
+ encodeURIComponent(txt.value);
};
};
main();
</script>
</head>
<body>
<div id="txtWrap">
<textarea id="txt"></textarea>
</div>
<!-- The download attribute sets the name of the file once downloaded -->
<a href="" id="link" download="code.txt">Download Above Code</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment