Hfyuu
Hfyuu

Reputation: 199

How to use javascript in specific jekyll posts?

I am trying to grab youtube title using oembed in jekyll.
This is my code with some errors like index.js: Unexpected token (3:12) in codepen. Please correct it.

<script>
          function getyoutubetitle(id) {
            var json = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3D' + id + '&format=json';
            document.write(json[title]);
          }
      </script>
getyoutubetitle(xh6mpAOD9ho);

Please see this link, Here I called javascript function specifically in post. But it is not working just gives corresponding text like getyoutubetitle(xh6mpAOD9ho);.

Upvotes: 0

Views: 125

Answers (2)

Keith Mifsud
Keith Mifsud

Reputation: 1618

Pass the argument as a string to start.

getyoutubetitle("xh6mpAOD9ho");

You can also use this node package https://www.npmjs.com/package/get-youtube-title

Seems a good fit for your use case since you only need the title. I don't think your function is getting any information. If you're not going to use the package, I suggest you look into using the google api. I am sure you'll find JS code snippets in docs.

Use the browser console to debug what the URL is fetching or use Postman to test the proper API.

Upvotes: 1

David Jacquel
David Jacquel

Reputation: 52829

  1. Javascript must be enclosed in <script> tag
  2. Id must be passed as a string, not as a variable name

Your code can look like :

<script>
getyoutubetitle("xh6mpAOD9ho");
</script>

Upvotes: 0

Related Questions