I’m trying to send a cookie containing the device dimensions. If the device is a mobile device/small screen, I want to prevent the image slider from loading. The purpose is to reduce the amount of HTTP requests for mobile devices.
The second pageload/refresh the slider is shown, so I know the code is working to some degree.
My questions are; is this best way to implement such function? And is it possible to read the cookie and output the slider on first page load?
I have done some reading and it appears that the page would need to be refreshed for the cookie to work. To me this seems self-defeating as it increases the page load speed. Could something be done with session-variable?
Here’s my code:
document.cookie = "device_dimensions=" + screen.width + "x" + screen.height;
</head>
$device_width = 0;
$device_height = 0;
// Read the device viewport dimensions
if (isset($_COOKIE['device_dimensions'])) {
$dimensions = explode('x', $_COOKIE['device_dimensions']);
if (count($dimensions)==2) {
$device_width = intval($dimensions[0]);
$device_height = intval($dimensions[1]);
}
}
if ($device_width > 0) {
if ($device_width >= 640) {
require('site/slider.php');
}
else if ($device_width = 0) {
require('site/slider.php');
}
}
And the working example: r.adamtoms.co.uk
Would really appreciate an explanation on how to solve this!
Adam