Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can I pass array object to javascript through script tag attribute from html?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading