Therefore I would like to code something along the lines of :
can anyone advise on the best way to achieve this please...
Look into techniques of "Progressive Enhancement", where your basic non-js site is a) navigable b) readable and makes sense then you progressively add JS to enhance the experience.
All client side dependencies such as is JS turned on happen on the client, PHP has already done its job and sent the stream of data to the client. So forget that idea now.