DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   javascript (http://www.daniweb.com/code/javascript.html)
-   -   Monitor Resolution Detection for Tables (http://www.daniweb.com/code/snippet413.html)

fsn812 javascript syntax
Oct 24th, 2005
A very simple PHP/Javascript snippet to detect the resolution of a user's monitor and create a table using that resolution to best fit within the user's browser (to avoid scrolling). Note: This is a PHP script which includes Javascript

  1. <?php
  2. $url = $_SERVER['PHP_SELF'];
  3.  
  4. if(isset($HTTP_COOKIE_VARS["res"]))
  5. $res = $HTTP_COOKIE_VARS["res"];
  6.  
  7. else{
  8. ?>
  9. <script language="javascript">
  10. <!--
  11. go();
  12.  
  13. function go()
  14. {
  15. var today = new Date();
  16. var the_date = new Date("August 31, 2020");
  17. var the_cookie_date = the_date.toGMTString();
  18. var the_cookie = "res="+ screen.width +"x"+ screen.height;
  19. var the_cookie = the_cookie + ";expires=" + the_cookie_date;
  20. document.cookie=the_cookie
  21. location = '<?echo "$url";?>';
  22. }
  23. //-->
  24. </script>
  25. <?php
  26. }
  27. ?>
  28. <?php
  29. //Let's "split" the resolution results into two variables
  30. list($width, $height) = split('[x]', $res);
  31.  
  32. //Take the width and height minus 300
  33. $tb_width = $width-300;
  34. $tb_height = $height-300;
  35.  
  36. //Make the table
  37. print("<table align=center border=1 width=" .$tb_width . " height=" . $tb_height . " >
  38. <tr><td align=center>Your screen resolution is " . $width . " by " . $height . ".<br>
  39. The width/height of this table is " . $tb_width . " by " . $tb_height . ".</td></tr>
  40. </table>");
  41. ?>