Your sites Log in Sign up Menu

How to "hard code" the JSON variables into a script tag when I need them in the JavaScript of a page

Following up on this question I would like to find out more in detail how to "hard code the JSON variables into a script tag" when I need to work with them in JavaScript.

For example the tags variable in the page's JSON structure, how can I access this in JavaScript? Do I read it out of the DOM , say, in the load event? Sorry, but I am a bit confused here.


a year ago, 2 replies   Improve this question

Something like this:

{{#entry}}
...
<script>
var author = "{{metadata.author}}";
alert('Author is' + author);
</script>
...
{{/entry}}

Does that make sense? You can even iterate over lists to generate arrays:

<script>
var tags = [ {{#tags}} { tag: "{{tag}}" }, {{/tags}} ];
</script>
Answered a year ago · Improve this answer

Thanks, David. Much appreciate your reply.

I can see this is essentially defining new variables in JS, not accessing the underlying JSON data, right?

Does this mean there is no way to _modify_the JSON data?

Answered a year ago · Improve this answer

Privacy Terms