I have been wrestling with this subject for six months and can find no complete answer anywhere on the web or in any book seen. I have become very familiar with the debate and so it is not necessary to debate the following here. I had rather do all of this in PHP, but I know of no way to avoid javascript because detection must take place on the client side, and no way to avoid AJAX because the feed is continuous and the pages fed depend on the viewport's width. Since the content of the pages will change, css at media will not work.
The object is first to create an html or, preferably, PHP page that will detect the ever-changing width of the viewport as the viewer crunches it up or extends it, and change the content of the page at two breakpoints; thus, there will be three php pages with content. I am not sure if the landing page should be .php or .html; in any case, the landing page will ultimately be the index page, and its sole function is to call or extinguish one of the three PHP includes, each of which will itself have includes and calls for the css. Both the content and the css of these includes may differ, operating on the principle of VARY HEADER in order to keep the integrity of the SEO.
In Pseudocode,the foregoing requirements become the following.
- Detect viewport width LIVE in javascript; thus, if a viewer diminishes or increases the width of the browser, the detection will be continuous.
- To make the detection constant, it seems that the javascript must not be cached.
- The output of the javascript, which is width of the viewport in pixels, must be passed to AJAX because the page is dynamic.
- I cannot find any coherent reference to conditional statements in AJAX, and I had rather avoid JQuery, but the idea is:-
if viewport < 700 px, serve x.php;
if viewport > 700 px and < 900 px, serve y.php;
if viewport > 900 px, serve z.php;- Since this must happen live on demand, it seems that the cache for the AJAX and the PHP pages must never be made.
- Whenever the viewer changes the viewport according to the above breakpoints, the PHP include will change, no matter how rapidly the viewer may do so. Practically, however, this means simply that telephones, tablets, and larger devices will each have a page tailored, both in style and in content, for that kind of device.
The above having been accomplished, the next considerations are:-
Is it possible to have the landing page written in php, which I doubt, because the detection ought to precede the PHP; hence, the probability arises that the landing page ought to be written in HTML.
Shall the html of the landing page be replaced by a completely fresh page, header and all, in one of the three called pages called by AJAX, such new page to be the usual PHP page in all of its glory? It seems doubtful, because the viewport must be constantly detected, which is the reason for the landing page in the first place. Therefore, each of the three called PHP pages must probably simply be includes that flesh out the original html page. If so, what is the best way in which to structure the code? Please see below for a statement of difficulties.
The first trial ought to be of simple content which, on each include, simply says something nice and gives the dimensions of the viewport live. If that works, then the frindly message and statement of width will be eradicated by replacement with the real content, a copy being kept as a souvenir for future reference.
I have cobbled together code in Javascript that detects the width and runs it live perfectly in all of the three named devices, and during crunching and expanding on a laptop, but I am having trouble preventing it from caching, so that it occasionally sticks on contraction when I reach a width of 541 px. Most websites seem to have this problem.
I have cobbled together AJAX code that will serve up the pages required, but I have not been able to destroy the caching.I have tried for many sleepless months to pass the running width from Javascript to AJAX, but without avail.
I can present either the width or the includes, but not both, on the landing page; the AJAX overpowers the Javascript and ignores its content.I tried to put the Javascript for detection on the include, which neither works, nor has any use, because the width must go to the AJAX before the includes are called.Although this question has been asked before in various ways, there is no one comprehensive answer to the question to be found.
Many thanks to any such of good will who may be kind enough to address this question with the comprehensiveness which it deserves.