I have written code to get the document resolution for a mobile screen resolution check. An event listener checks client width and if resolution <= 768 php var should becomes true
else false
.
<script>
window.addEventListener("resize", function (e){
var width = document.body.clientWidth;
console.log(document.body.clientWidth);
if (width <= 768) {<?php $mobileScreenRes = true; ?> console.log("if: " + <?php echo json_encode($mobileScreenRes); ?>); }
else {<?php $mobileScreenRes = false; ?>console.log("else : " + <?php echo json_encode($mobileScreenRes); ?>); }
console.log("final:" + <?php var_export($mobileScreenRes); ?>);
console.log("isset :" + <?php echo isset($mobileScreenRes); ?>);
});
</script>
I can get width, but can not use this how I want. $mobileScreenRes variable is a global variable. Console output is:
795
VM45496:6 else : false
VM45496:7 final : false
VM45496:8 isset : 1
582
VM45496:5 if : true
VM45496:7 final :false
VM45496:8 isset : 1
I can not understand why the final is always false, even with the if statement?