I'm trying to access a PHP variable in a JS file. The js file is appended at the end of the page.
Now I can only think of 2 possible options, but both seem kind of hacky and my question is, what are the pros & cons of both approaches (or is there a better way?).
Option 1: use inline scripts in my HTML file to set a variable. e.g.
<body>
<script type="text/javascript">
var link = "<?= $var ?>";
</script>
</body>
Option 2: use HTML data-attributes to assign the variable to an HTML container, and later retrieve it with jQuery. e.g.
In HTML file:
<body>
<div id="container" data-ajaxVar="<?= $var ?>">
Random html
</div>
</body>
In JS file:
var link = $("#container").data("ajaxVar");
Any suggestions?