Home > Software engineering >  can I pass array object to javascript through script tag attribute from html?
can I pass array object to javascript through script tag attribute from html?

Time:11-11

I'm trying to pass an object (which ultimately I want to be an array but using string for now) like this:

<script> var myparameter="myvalue";
</script>

<script src="app.js"  data-par="michael" data-site=myparameter type="module"></script>

The other attribute ("michael") does work, but I can't pass the value of myparameter because it passes the string myparameter instead of the value of the variable.

Is there away to pass an object (or array) to the javascript script?

By the way, ultimately the values of the array would be too long to fit in one line as a tag attribute anyway, which is why I want to initialize it as a variable to be passed

CodePudding user response:

use template literal

data-site=`${myparameter}`
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

In your app.js you can will have access to the var myparameter. Because it is on the global scope.

  • Related