How do I use the value of a JavaScript variable in a module?
You could send that value to a callback function in your .module file using AJAX, an example to do that stuff in forms see that tutorial Ajaxify form fields  

And you could send any value to js variable see sending-a-javascript-variable-to-drupal