I’m developing a pet project with jQuery. Now the application requires some variable value is transferred to client javascript when the page is loaded. I’m wondering what’s the best practice to do it.
I can image to do it in two ways. First, render it to be a javascript variable in the page.
<script> <?php echo "var maxid = $maxid;"?> </script>
Which means the client will see
<script> var maxid = <somevar>; </script>
Second, assign it to be an attribute of one element.
<div maxid="<php echo $maxid >" />
Which approach is better? Is there any other way to do it?
For the sake of valid html I’d go with the first method, though it really doesn’t matter as long as it gets the job done.