简体   繁体   English

如何在javascript和/或php中获得访问者的屏幕分辨率?

[英]How to get screen resolution of visitor in javascript and/or php?

I would like to know the screen resolution of a visitor visiting my page so I can properly make a jquery thickbox cover about 75% of their screen. 我想知道访问我页面的访问者的屏幕分辨率,因此我可以正确地制作一个jQuery厚盒覆盖约75%的屏幕。

Solutions to the question or helping me solve my problem are greatly appreciated! 非常感谢您解决问题或帮助我解决问题的方法!

In JavaScript it's: 在JavaScript中,它是:

screen.width
screen.height

But PHP has no access to the client. 但是PHP无法访问客户端。 So you need to pass the values gathered by JavaScript to your PHP script. 因此,您需要将JavaScript收集的值传递给PHP脚本。

Additionally not every user has his windows in full screen mode. 此外,并非每个用户的窗口都处于全屏模式。 So you should better use the available window width/height . 因此,您最好使用可用的窗口width / height

If you are using jQuery, there is cross-browser solution to get the browser window size: 如果您使用的是jQuery,则可以使用跨浏览器解决方案来获取浏览器窗口大小:

var browserWidth = $(window).width();
var browserHeight = $(window).height();

What makes you think people always have their browser window maximised to the same size as their screen? 是什么让您认为人们总是将浏览器窗口最大化到与屏幕大小相同? I don't, and I'm not the only one. 我不是,我不是唯一的一个。

What you want is to cover 75% of their window . 您想要遮盖其窗户的 75%。 This can be done using CSS; 可以使用CSS来完成; if the element in question is a child of the body element, then 如果所讨论的元素是body元素的子元素,则

#box {
    position: absolute;
    top: 12.5%;
    left: 12.5%;
    width: 75%;
    height: 75%;
}

will probably do it (I've not tested it, though). 可能会做到(尽管我还没有测试过)。

EDIT: I've now tested it, adding 编辑:我现在已经对其进行了测试,添加

html, body {
    width: 100%;
    height: 100%;
}

and it worked as expected, at least in Firefox 3.5 :-) 并按预期工作,至少在Firefox 3.5中:-)

you can try this Window dimensions Hack and it speaks to ALL Browsers alike. 您可以尝试使用此窗口尺寸Hack,它对所有浏览器都适用。 (including dumb IE6) (包括愚蠢的IE6)

var width = document.body.offsetWidth;
var bodyHeight = document.body.scrollHeight;
var height = (typeof window.innerHeight !== "undefined")? window.innerHeight :  (document.documentElement)? document.documentElement.clientHeight : document.body.clientHeight;
    height = (bodyHeight > height)? bodyHeight : height;

Well after all the work done on the subject I will improve my answer.. 在完成该主题的所有工作之后,我将改善我的答案。

First some conclusions that have been earned with much effort: 首先,我们需要付出很多努力才能得出一些结论:

  1. [No cookies.. forget it] Don't do anything like setting session variables on serverside. [没有cookie ..算了吧]不要在服务器端设置会话变量。 Otherwise evrey time your page loads a new session file will be created on serverside (file is the usual handler for sessions) and the directory will pile up with files and you still wont be able to track the session.If you find a way, we all will be happy to hear it. 否则,您的页面将在服务器端创建一个新的会话文件(文件是会话的常用处理程序),并且目录将堆满文件,您仍然无法跟踪会话。所有人都会很高兴听到它。

  2. [No cookies.. forget fancy scripts!] Without cookies you might have some success, like with the method of tracking ip etc, but still errors are there and there will be a circumstance where it will appear. [没有cookie ..忘记花哨的脚本!]如果没有cookie,您可能会取得一些成功,例如使用跟踪ip等方法,但是仍然存在错误,并且在某些情况下它会出现。

  3. [No cookies.. go for no cookies] If you can have your pages work without cookie go for it otherwise is not worth. [没有cookie ..不获取任何cookie]如果您可以使页面在不使用cookie的情况下工作,那么就不值得了。

As for screen resolution my script below can do these: 至于屏幕分辨率,我的以下脚本可以做到这些:

  1. Immediately display info for all 4 combinations cookie and js enabled. 立即显示所有启用了cookie和js的4种组合的信息。
  2. If the user makes a change enabling-disabling js or cookie these changes are identified immediately by the script. 如果用户进行更改以启用或禁用js或cookie,则这些更改将立即由脚本识别。
  3. I used chrome plus and any change I did, in any combination was immediately identified. 我使用chrome plus,并立即发现了我所做的任何更改。 It worked even without deleting old cookies. 即使没有删除旧的Cookie,它也能正常工作。

This is my script, it includes a dbg() function just for debugging, I have tried to make it error free however test for your self! 这是我的脚本,它包括一个仅用于调试的dbg()函数,我尝试使其无错误,但是请对您的自我进行测试!

<?PHP




class prereqCls
{

   //change this
  private $self="http://localhost/prereq.php";
  private $jsCookie="";

  function __construct()
  {     


    //entering conditions, an autosubmit bttn fetches the rest fields not it self 
    $enter=false;
    if(isset($_GET['rl']))$enter=true;
    if(!isset($_POST['js_alive']) and !isset($_POST['noJsBttn']))$enter=true;


    if($enter)
    {
      dbg("Now writing a php cookie..","construct");
      setcookie('cookie_alive',1,time()+3600);//set it for 1 hour 
      dbg("Now calling Js..","construct");
      $this->getSettings();
      return;
    }



    //identify setting
    $this->identifySett();


    //display the appropriate link  
    if($this->jsCookie!=="")
              echo "<a href=".$this->self."?rl=1 >If you enebled {$this->jsCookie}, don't refresh 
                but click here to load the new settings!</a>";
    else
              echo "<a href=".$this->self."?rl=1 >Click only this to relaod the page..</a>"; 

    //display the cookie if set
    if(isset($_COOKIE['cookie_alive']))
    {
      echo "<br><br>Well this is the cookie!<br>";
      print_r($_COOKIE);
    }


  }//method



  private function identifySett()
  {
        if(isset($_POST['js_alive']) and isset($_COOKIE['cookie_alive']))
      dbg("Both js and cookies are enabled!","construct");

    elseif(isset($_POST['js_alive']) and !isset($_COOKIE['cookie_alive']))
      {dbg("Js is enabled cookies not!","construct"); $this->jsCookie="Cookies";}

    elseif(!isset($_POST['js_alive']) and isset($_COOKIE['cookie_alive']))
      {dbg("Js is not enabled, cookie is!","construct");  $this->jsCookie="Javascript";}

    elseif(!isset($_POST['js_alive']) and !isset($_COOKIE['cookie_alive']))
      {dbg("Js and cookies are not enabled!","construct"); $this->jsCookie="Javascript and Cookies";}

  }


//php method begins
private function getSettings()
{
?>
<html>
<head>
<script type="text/javascript">

//the values entered to cookie by js will be seen by php immediately on reload!!! 
//write the screen data to the cookie too 
var exdays = 365; 
var exdate=new Date(); 
exdate.setDate(exdate.getDate() + exdays);

var c_value=escape(screen.width) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie="scr_width=" + c_value;

var c_value=escape(screen.height) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie="scr_height=" + c_value;


var c_value=escape(1) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie="js=" + c_value;

//autosubmit the form in milliseconds
window.setTimeout("document.getElementById('scrForm').submit();",0);


</script>


</head>

<body >

 <form id="scrForm" action="<?PHP echo $this->self;?>" method="post">   

<!--If Js is enabled the appropriate bttn will be shown-->
   <script type="text/javascript">
   document.write('<input type="hidden" name="js_alive" value="1">');
   document.write('<input type="submit" name="JsBttn" value="Go">');
   </script>

 <noscript>
 Some trouble trying to find screen settings.. Click to continue!
 <input type="submit" name="noJsBttn" value="Go">
 </noscript>

 </form>  

</body>
</html>

<?PHP

}
////////////////////php method ends

}/////////////class




//debuger
function dbg($str,$caller)
{ 

echo "<br> [{$str}][{$caller}]<br>";

$log="Log Begins:---".date('Y-M-d h:i:s')."--------[CALLER]{$caller}----------\n";

$log.= $str."\n";

$log.= "Log Ends:-------------------------------------------------------\n\n";

$fh=fopen("log_errors.log","a");
fwrite($fh, $log);
fclose($fh);
}
///////////////////// 




//make the class alive
$alive = new prereqCls();






?> 

After you identify screen you can go for appearance like this: 识别屏幕后,您可以进行如下显示:

In your view.htm (whatever) file include style like this: 在您的view.htm(无论如何)文件中,包括以下样式:

   <link rel="stylesheet" href="style.php" />

then your style.php could be like this: 那么您的style.php可能是这样的:

<?php
    header("Content-type: text/css");

    $width  = $_COOKIE['scr_width']; 
    $height = $_COOKIE['scr_height']; 

element 1 width is let's say 0.2 of width
element 2 width is let's say 0.7 of width
big container (it could be a single cell table) width=all other elements width
or
if image height is>image width then this variable will be ...
or
image path is random
or image path dynamic
or ... etc etc endless possibilities, you can do anything here in this css stylesheet.
?>

then [still on the same file, Yes css and php together!] for example apply the variables,
only simple variables work down here, keep calculations for above.



#smImgZoom
{
width:<?PHP echo $zoomerImgWidth."px;";?> 
height:<?PHP echo $zoomerImgHeight."px;";?> 
    padding:10px ;
}  

etc etc this is how I do it so html remain clear, css clear, calculations separated.

Just my way, it is not necessary good! 只是我的方式,没有必要好!

hey i found out that there's a useful topic related to this, 嘿,我发现有一个与此相关的有用主题,

Difference between screen.availHeight and window.height() screen.availHeight和window.height()之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM