title: How to run javascript with no filename
body: <p>I am new to coding so I have a question regarding Jupyter Notebook and the use of Javascript. A snippet of my current code is as follows (trueName is defined prior to this code and <code>pickle.dump</code> is usually tabbed, but isn't here due to stack overflow's formatting):</p>
  
  <hr>
  
  <pre><code>%%javascript
  var kernel = Jupyter.notebook.kernel;
  console.log(kernel);
  var command = "import pickle" + "\n" + "file_name = 'testfile'" + "\n" + "with open(file_name, 'wb') as my_file_obj:" + "\n" + "pickle.dump(trueName,my_file_obj)";
  kernel.execute(command);
  </code></pre>
  
  <hr>
  
  <p>This works fine but for some reason when I place it into the following format:</p>
  
  <pre><code>from IPython.core.display import Javascript
  Javascript("""
  var kernel = Jupyter.notebook.kernel;
  console.log(kernel);
  var command = "import pickle" + "\n" + "file_name = 'testfile'" + "\n" + "with open(file_name, 'wb') as my_file_obj:" + "\n" + " "pickle.dump(trueName,my_file_obj)";
  kernel.execute(command);""")
  </code></pre>
  
  <p>I obtain the following error even though the code is the same: </p>
  
  <blockquote>
    <p>Javascript error adding output!
    SyntaxError: Invalid or unexpected token
    See your browser Javascript console for more details.</p>
  </blockquote>
  
  <hr>
  
  <p>I had hoped to use the second method in order to bypass the magic command barrier in using something similar to <code>!ipython somefile.ipy</code>
  to read the javascript, but for some reason the second method really doesn't like the <code>var command</code> something I discovered after much testing. I have a few questions that I would greatly appreciate if I received some feedback on:</p>
  
  <ol>
  <li>Why does the second method provide an error due to my <code>var command</code>? I cannot figure out why this happens.(Is it true that I have to use 'textarea'? If so, how do I do this? I became lost when trying to do so myself: <a href="https://stackoverflow.com/questions/15357846/how-to-force-a-line-break-on-a-javascript-concatenated-string">How to force a line break on a Javascript concatenated string?</a>)</li>
  <li>Is there a method for me to run Javascript magic when not directly in the notebook? I have tried running ipython on the code in a separate file with the <code>.ipy</code> ending as seen above, but it does not like running the cell magic nor the import from IPython.core.display. (This method does not work for me: <a href="https://stackoverflow.com/questions/10361206/how-to-run-an-ipython-magic-from-a-script-or-timing-a-python-script">How to run an IPython magic from a script (or timing a Python script)</a>)</li>
  <li>Is there a way for me to execute Javascript code directly (not via a file) when using a function such as  <code>def run_javascript(code):</code>? Executing a cell in Jupyter gives back the code within under the argument <code>code</code>, but I cannot find out how to run it. I have been looking around but the only answers I have found are about <code>!node somefile.js</code> and other similar filename based Javascript codes.
  I would appreciate any help! I did have a few suggestions as to how to run python code as I would like to, but since IPython doesn't work with the Javascript I am at a loss (Python Tips: <a href="https://stackoverflow.com/questions/15587877/run-a-python-script-in-terminal-without-the-python-command">run a python script in terminal without the python command</a> <a href="https://stackoverflow.com/questions/10768584/execute-python-commands-passed-as-strings-in-command-line-using-python-c">Execute python commands passed as strings in command line using python -c</a>) </li>
  </ol>