0

I'm trying to make an app that allows you a text editor. You can type anything in to a text block. You can do two things with said text block:
a) run the text as an html page in a new about:blank tab or
b) save the text as a .html
For some reason, though, when I tried to implement the save ability, neither function would load. If I go into the JS console, it shows me this error upon clicking on the save button:

"Uncaught ReferenceError: saveAsFile is not defined (temp.html,1)"

When I seperated them into two <script> blocks, I could get the save function to work. However, when I did that, the Run in New Tab function no longer worked. It was utterly confusing.
I have used multiple functions before, and I don't know why it's suddenly not working. Can someone help? This is my code:

<script>
function run() {
var codeTab = window.open("" _blank);
var codeRun = document.getElementById("code").value;
codeTab.document.write(codeRun);
}
</script>
<script>
/*i stole-i mean used-this code from someone else*/
function saveAsFile() {
var textToSave = document.getElementById("code").value;

var hiddenElement = document.createElement('a');

hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'save.html';
hiddenElement.click();
}
</script>

1 Answer 1

0

On line 3, you have window.open("" _blank);. It should be window.open("_blank");.

I also cannot see anywhere saveAs() is being called (or defined). Is it possibly being called in codeRun? See below code which worked for me.

<textarea id="code">

</textarea>
<button onclick="run();">Run</button>
<button onclick="saveAsFile();">Save</button>

<script>
  function run() {
    var codeTab = window.open("_blank");
    var codeRun = document.getElementById("code").value;
    codeTab.document.write(codeRun);
  }

  /*i stole-i mean used-this code from someone else*/
  function saveAsFile() {
    var textToSave = document.getElementById("code").value;

    var hiddenElement = document.createElement('a');

    hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
    hiddenElement.target = '_blank';
    hiddenElement.download = 'save.html';
    hiddenElement.click();
  }
</script>
6
  • Though this is useful, and I did forget to put _blank in quotes, I did intentionally leave those two quotes empty - the first set of quotation marks is for the website url, and leaving those empty defaults to about:blank
    – thanoschimichan-ga
    Oct 30, 2019 at 20:53
  • I tried the code you gave and got ReferenceError: run is not defined when clicking run. After correcting that, it was working. I can update my answer with the exact code I used if you like.
    – DCowan
    Oct 30, 2019 at 21:53
  • @DCowan - You received a different error than what the author received, which is explained by the fact, they left the URL string as an empty string (on purpose). Oct 30, 2019 at 22:23
  • Yes. I do notice that I can't see anywhere @thanoschimichan-ga is calling a function or accessing a variable saveAs. That does lead me to believe that it may be something in code @thanoschimichan-ga has put in codeRun.
    – DCowan
    Oct 30, 2019 at 22:27
  • Sorry - saveAs is saveAsFile, I typed it wrong - I'll edit the original post
    – thanoschimichan-ga
    Oct 30, 2019 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.