I have found several posts across the web which describe how to detect javascript using PHP and I have already put together my own solution together (below) but...
...I am wondering if I can improve on what I have already.
SUMMARY
1) I want to load a mobile-optimised page as quickly as possible.
2) For .js-enabled browsers I want to load the entire page in (less accessible but faster loading) Configuration B - with the intention of later (ie. after onload) reconfiguring parts of the page - consequent to the user triggering various javascript events - as (more accessible) Configuration A.
3) For non-.js-enabled browsers, I want to load the entire page directly in (more accessible) Configuration A.
SOLUTION (so far)
The following .js <script> is as high up in the <head> as I dared place it (just below the <title>).
<?php
echo '
<script>
if (window.location.href != \'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?enhance=js\') {
window.location.href = \'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?enhance=js\';}
</script>';
$hasJS = 0; if (in_array('js',$_GET)) {$hasJS++;}
?>
As you can see, the <script> tells .js enabled browsers to check for a specific GET parameter appended to the URI (enhance=js) and if the parameter is absent, to reload the page immediately with that GET parameter appended to the URI.
PHP then checks if the GET parameter exists and if it does, a flag, $hasJS, is switched on to tell the rest of the PHP script that the browser is .js-enabled.
So far, so good. It works.
But can I improve on this .js detection via PHP method?
Questions:
1) Is there a way to do it which doesn't involve changing the URI or reloading the page?
2) Apart from the URI, is there any other page-related attribute to which I can append data with javascript before retrieving that same data with PHP?