├── README.md
├── content.php
├── index.php
├── mobile-mustard.js
├── screen-large.php
├── screen-small.php
├── style-large.css
├── style-small.css
└── style.css
/README.md:
--------------------------------------------------------------------------------
1 | # server-side-mustard-cut
2 |
3 | JavaScript, cookies, and a possible reload to get a server-side choice of documents.
4 |
5 | This is what we're currently doing for the [CodePen Editor](http://codepen.io/pen/) so I wanted to document it, since it seems to be working pretty well.
6 |
7 | Here's a demo.
--------------------------------------------------------------------------------
/content.php:
--------------------------------------------------------------------------------
1 | For simplicities sake, our mustard-cut here will be a measuring of the screen width. But it could test for anything client-side. We're also using PHP here because easy, but it could be any server side language.Server Side Mustard Cutting
3 |
I'm not saying this is the best possible solution. I am saying this seems reasonable to me and I'm using it in production.
21 | 22 |This kind of thing has been approached before. These things aren't necessarily mutually exclusive.
23 | 24 |Server side:
55 | 56 |<?php
57 |
58 | // This is just a FAKE ROUTER
59 | // Do this however your site does routing
60 |
61 | if (isset($_COOKIE["screen-width"])) {
62 |
63 | // Large screen
64 | if ($_COOKIE["screen-width"] > 700) {
65 |
66 | include_once("screen-large.php");
67 |
68 | // Small screen
69 | } else {
70 |
71 | include_once("screen-small.php");
72 |
73 | }
74 |
75 | // Choose a default
76 | } else {
77 |
78 | include_once("screen-small.php");
79 |
80 | }
81 |
82 | ?>
83 |
84 | In the document that gets served, if the cookie isn't already present, run the mustard cutting script:
85 | 86 |<?php
87 | // Run this script as high up the page as you can,
88 | // but only if the cookie isn't already present.
89 | if (!isset($_COOKIE["screen-width"])) { ?>
90 | <script src="mobile-mustard.js"></script>
91 | <?php } ?>
92 |
93 | The script is:
94 | 95 |(function() {
96 |
97 | // If the browser supports cookies and they are enabled
98 | if (navigator.cookieEnabled) {
99 |
100 | // Set the cookie for 3 days
101 | var date = new Date();
102 | date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
103 | var expires = "; expires=" + date.toGMTString();
104 |
105 | // This is where we're setting the mustard cutting information.
106 | // In this case we're just setting screen width, but it could
107 | // be anything. Think http://modernizr.com/
108 | document.cookie = "screen-width=" + screen.width + expires + "; path=/";
109 |
110 | /*
111 | Only refresh if the WRONG template loads.
112 |
113 | Since we're defaulting to a small screen,
114 | and we know if this script is running the
115 | cookie wasn't present on this page load,
116 | we should refresh if the screen is wider
117 | than 700.
118 |
119 | This needs to be kept in sync with the server
120 | side distinction
121 | */
122 | if (screen.width > 700) {
123 |
124 | // Halt the browser from loading/doing anything else.
125 | window.stop();
126 |
127 | // Reload the page, because the cookie will now be
128 | // set and the server can use it.
129 | location.reload(true);
130 |
131 | }
132 |
133 | }
134 |
135 | }());
136 |
137 | ୧༼ʘ̆ںʘ̆༽୨
138 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 700) { 9 | 10 | include_once("screen-large.php"); 11 | 12 | // Small screen 13 | } else { 14 | 15 | include_once("screen-small.php"); 16 | 17 | } 18 | 19 | // Choose a default 20 | } else { 21 | 22 | include_once("screen-small.php"); 23 | 24 | } 25 | 26 | ?> -------------------------------------------------------------------------------- /mobile-mustard.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | (function() { 4 | 5 | // If the browser supports cookies and they are enabled 6 | if (navigator.cookieEnabled) { 7 | 8 | // Set the cookie for 3 days 9 | var date = new Date(); 10 | date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000)); 11 | var expires = "; expires=" + date.toGMTString(); 12 | 13 | // This is where we're setting the mustard cutting information. 14 | // In this case we're just setting screen width, but it could 15 | // be anything. Think http://modernizr.com/ 16 | document.cookie = "screen-width=" + window.outerWidth + expires + "; path=/"; 17 | 18 | /* 19 | Only refresh if the WRONG template loads. 20 | 21 | Since we're defaulting to a small screen, 22 | and we know if this script is running the 23 | cookie wasn't present on this page load, 24 | we should refresh if the screen is wider 25 | than 700. 26 | 27 | This needs to be kept in sync with the server 28 | side distinction 29 | */ 30 | if (window.outerWidth > 700) { 31 | 32 | // Halt the browser from loading/doing anything else. 33 | window.stop(); 34 | 35 | // Reload the page, because the cookie will now be 36 | // set and the server can use it. 37 | location.reload(true); 38 | 39 | } 40 | 41 | } 42 | 43 | }()); 44 | -------------------------------------------------------------------------------- /screen-large.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 14 |