resolution - Get user's screen&viewport dimensions in php on first load -
i want in php height/width of both screen , viewport when user visits page.
i've tried different ways cause other problems.
my goals:
get info on first load (no jump page, no reload).
not change url
not affect rest of php runs on load, or make php run twice
what i've tried far:
javascript viewport dimensions:
if(!isset($_post['width']) || !isset($_post['height'])) { echo ' <script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { var height = $(window).height(); var width = $(window).width(); $.ajax({ type: \'post\', data: { "height": height, "width": width }, success: function (data) { $("body").html(data); }, }); }); </script> '; } $user_width = $_post['width']; $user_height = $_post['height'];
problems: causes php run twice, once on load, once when returns value (the second time 4 seconds after first), makes rest of php wacky... also, makes page load slow
putting screen dimensions url:
if(isset($_session['screen_width']) , isset($_session['screen_height'])){ $user_width = $_session['screen_width']; $user_height = $_session['screen_height']; } else if(isset($_request['width']) , isset($_request['height'])) { $_session['screen_width'] = $_request['width']; $_session['screen_height'] = $_request['height']; header('location: ' . $_server['php_self']); } else { echo '<script type="text/javascript">window.location = "' . $_server['php_self'] . '?width="+screen.width+"&height="+screen.height;</script>'; }
problems: changes url (which affects other code , looks bad compared expressionless urls have now); doesn't return viewport size afaik
i'm noob. maybe it's easy do, don't know. searched lot. that's how got methods above. couldn't them work me. afaik, haven't seen solution anywhere similar situation.
thanks :)
how having document ready ajax request send separate php document, include in head on page load. way, can have whatever specific html want loaded put straight body when ready.
for example:
<head> <script> $(document).ready( function() { var height = $(window).height(); var width = $(window).width(); $.ajax({ url: 'body.php', type: 'post', data: { 'width' : width, 'height' : height, 'recordsize' : 'true' }, success: function(response) { $("body").html(response); } }); }); </script> </head> <body></body>
and body.php
file like:
<?php if(isset($_post['recordsize'])) { $height = $_post['height']; $width = $_post['width']; $_session['screen_height'] = $height; $_session['screen_width'] = $width; //any html want returned in here
if didn't want js function run every page load, encase whole thing in <?php if(isset($_session['screen_height'])) { //js function } ?>
i'm not 100% sure intending dimensions once have them, apologise if answer little vague, doing way page sets saved dimensions on first visit browsing session, , doesn't have redirect, hope it's @ least little helpful :)
Comments
Post a Comment