Hi guys,
I’ve to pick data from a PHP function using one of the methods like below, but I can’t decide which one is better:
<script>
var data = "<?php echo $data; ?>";
(function blahBLAHandBlah() {
// do something with data
})();
</script>
VS
<div id="data-item">
<?php echo $data; ?>
</div>
<script>
(function blahBLAHandBlah() {
var data = document.getElementById('data-item'),
value = data.innerHTML;
// do something with data
})();
</script>
The data would be fed into a JavaScript function in order to process it.
Someone has said global variable is bad.
The others say don’t touch DOM.
In term of better performance, which one is better. I’ve to get data from method like these all over the site.
PS. The data element will not be shown.
Thank you,