0

I have a javascript that is supposed to create a download link to a text file when a button is clicked but I am getting an "Invalid left-hand side expression" error with it.

The js looks like this:

(function () {
  var textFile = null,
  var 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');

  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile({{request_string}});
    link.style.display = 'block';
  }, false);
})();

I've also tried doing this:

var text_string = {{request_string}}
link.href = makeTextFile(text_string);

Where {{request_string}} looks like this:

----------------Request File Generated 2015-06-30 17:23:16.324439------------------
     WINDOW_START    |      WINDOW_END     |       DURATION      |         SAT         |         TYPE        |       PRIORITY      |             
--------------------------------------------------------------------------------
 2015/01/02 00:00:00 | 2015/01/22 00:00:00 |         500         |          F7         |       NOMINAL       |          5          |  

And the html looks like this:

<div class="panel panel-body">
      <button id="create">Create file</button> 
      <a download="info.txt" id="downloadlink" style="display: none">Download</a>
</div>

The {{request_string}} is type str so I'm not sure what the problem is. Can anyone tell me what I'm doing wrong?

Thanks!!

kdubs
  • 808
  • 1
  • 19
  • 39
  • 1
    Does `{{request_string}}` expand to something with proper quotes in it? If not you may have invalid JavaScript. – tadman Jun 30 '15 at 17:35
  • Is the error message not linked to a specific line of code? – Álvaro González Jun 30 '15 at 17:35
  • What do you mean, "*`{{request_string}}` looks like this*"? Where does it come from? Are you using a templating engine on your script file or what? If so, which one? – Bergi Jun 30 '15 at 17:35
  • That looks like a templating engine is used like @Bergi says so you need to quote it: `link.href = makeTextFile("{{request_string}}");` – marekful Jun 30 '15 at 17:36
  • @marekful: That's usually not enough, you also need to escape it. Rather `makeTextFile({{json_stringify(reqest_string)}})` (notice the stringification is done by the templating engine) – Bergi Jun 30 '15 at 17:39
  • please replicate it in jsfiddle ... – Uday Shankar Jun 30 '15 at 17:40
  • Adding quotes around it fixed the invalid expression error, but now I'm getting the error: Cannot read property 'addEventListener' of null. To clarify, this js is being used inside an html template that is used in a django project. `{{request_string}}` is part of a dictionary passed to the template from a django view. – kdubs Jun 30 '15 at 17:42
  • 1
    @klwahl: That's probably related to [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/q/14028959/1048572) – Bergi Jun 30 '15 at 18:03
  • @Bergi that solved it. Thanks a ton! – kdubs Jun 30 '15 at 19:02

1 Answers1

0

I ended up using document.getElementsByTagName('pre') instead of trying to pass in a variable containing the string.

kdubs
  • 808
  • 1
  • 19
  • 39