IMO, the logic is flawed.
For one, there's no way to detect the screen width with PHP.
Secondly, if you were to generate a PHP -> JS file (my-script.js.php
), you'd have to load WordPress engine two times.
The Answer in this link is super-pro. You'll find easier methods that use wp-load.php
and WP_USE_THEMES
.
Thirdly, the way you're trying to load a theme template would require the "load two times" approach. Or solving it with AJAX and wp_localize_script
.
If I understood the Question correctly, I think the best bet would be to use WordPress user-agent detection and enqueue your scripts and styles accordingly. Study the core function to adapt to your needs.
[update]
Still not sure if all this complexity is really needed. Would something like this work in a generic header.php
?
if( wp_is_mobile() )
wp_nav_menu(MOBILE);
else
wp_nav_menu(DESKTOP);
But, if you really need this changes to happen dynamically, then AJAX would be the choice.