i have a script on my html page like that:
<script>
let a = <%-JSON.stringify(tags)%>
</script>
now I need this variable on my main.js file. But I don't know how I can access it
CodePudding user response:
You can access it with window.a
, as long as that script tag is above the script tag for main.js
, but this requires a different way to set the value at the start.
<script>
window.a = <%-JSON.stringify(tags)%>;
</script>
access in your code like
let foo = a.bar;