Home > Enterprise >  how to pass ejs variable to extern js script
how to pass ejs variable to extern js script

Time:06-28

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;
  • Related