shragi
asked on
Javascript create file issue
Hi - I am using below code to create text file and I looks like it is working in demo sites (http://jsfiddle.net/uselesscode/qm5ag/)
but not in my machine, what am I missing ?
but not in my machine, what am I missing ?
<!DOCTYPE HTML>
<html>
<head>
<title>File Create</title>
<meta name="description" content="website description" />
<script type= text/javascript>
(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create'),
textbox = document.getElementById('textbox');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();
</script>
</head>
<body><textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>
</body>
</html>
Thanks,
Just as an asside - you don't need the type="text/javascript">
<script>
...
</script>
Is perfectly acceptable.
Do you get any errors.
When you say it is not working - do you mean the code works for you when in the Fiddle but when you move the code to your dev machine it does not work?
Are you able to download the file if you browse from your machine to the fiddle?
<script>
...
</script>
Is perfectly acceptable.
Do you get any errors.
When you say it is not working - do you mean the code works for you when in the Fiddle but when you move the code to your dev machine it does not work?
Are you able to download the file if you browse from your machine to the fiddle?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Aurelian
script inside the head tags is fine. In fact, you'll find the script inside the head tags more often than not.
The problem with the OP's original code is that text/javascript type was not in quotes. As Julian mentioned, the type is not required. But it certainly is an error to include it without surrounding it with quotes.
script inside the head tags is fine. In fact, you'll find the script inside the head tags more often than not.
The problem with the OP's original code is that text/javascript type was not in quotes. As Julian mentioned, the type is not required. But it certainly is an error to include it without surrounding it with quotes.
Open in new window
should be
Open in new window