Embed javascript in markdown

Different solution that might work in some cases: (the selected answer didn't work for me when I was trying to embed a CodePen example)

  • add this to your default layout:

    <!-- Custom JavaScript files set in YAML front matter -->
    {% for js in page.customjs %}
    <script async type="text/javascript" src="{{ js }}"></script>
    {% endfor %}
    
  • In posts where you need some JavaScript files, you can add them in the YAML front matter like so:

    ---
    layout: post
    title: Adding custom JavaScript for a specific post
    category: posts
    customjs:
     - http://code.jquery.com/jquery-1.4.2.min.js
     - http://yourdomain.com/yourscript.js
    ---
    

The async might not be necessary or wanted but you could probably add that as a parameter in customjs. (see YAML front matter for Jekyll and nested lists for details)


I use this code to write JavaScript code in markdown.

just add js after "```" , and you'll have your JavaScript code highlighted.


 ```js
   const myvar = "hello"
   module.exports.response = response = ()=>{mycode here}
    ```

I had this same problem, but I managed to get JavaScript to appear in my code by putting a newline after the opening tag.