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

我想知道访问我的页面的访问者的屏幕分辨率,所以我可以正确地制作jquery厚箱封面大约75%的屏幕。

我非常感谢您提出解决问题的方法或帮助我解决问题!

是什么让您认为人们的浏览器窗口最大化与屏幕大小相同? 我没有,我不是唯一一个。

你想要的是覆盖他们75%的窗口 。 这可以使用CSS完成; 如果有问题的元素是body元素的子元素,那么

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

可能会这样做(我没有测试过它)。

编辑:我现在测试它,添加

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

并且它按预期工作,至少在Firefox 3.5中:-)

在JavaScript中它是:

 screen.width screen.height 

但PHP无法访问客户端。 因此,您需要将JavaScript收集的值传递给PHP脚本。

此外,并非每个用户的窗户都处于全屏模式。 所以你最好使用可用的窗口宽度/高度 。

如果您使用的是jQuery,则可以使用跨浏览器解决方案来获取浏览器窗口大小:

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

你可以尝试这个窗口维度哈克,它会对所有浏览器都说话。 (包括哑巴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; 

在完成关于这个主题的所有工作后,我将改进我的答案..

首先,我们付出了很多努力的结论:

  1. [没有cookie ..忘记了]不要做任何事情,比如在服务器端设置会话变量。 否则你的页面加载时会在服务器端创建一个新的会话文件(文件是会话的常用处理程序),目录将堆积文件,你仍然无法跟踪会话。如果你找到了办法,我们所有人都乐于听到。

  2. [没有cookie …忘记花哨的脚本!]没有cookie你可能会有一些成功,比如跟踪ip等方法,但仍然存在错误,并且会出现一种情况。

  3. [没有cookies..没有cookies]如果你可以让你的网页没有cookie工作,那么就不值得。

至于屏幕分辨率,我的脚本可以执行以下操作:

  1. 立即显示所有4种组合cookie和js启用的信息。
  2. 如果用户进行更改启用 – 禁用js或cookie,则脚本会立即识别这些更改。
  3. 我使用了chrome plus和我所做的任何改变,立即确定了任何组合。 它甚至没有删除旧的cookie也工作。

这是我的脚本,它包含一个dbg()函数只是为了调试,我试图让它没有错误,但测试你自己!

 getSettings(); return; } //identify setting $this->identifySett(); //display the appropriate link if($this->jsCookie!=="") echo "If you enebled {$this->jsCookie}, don't refresh but click here to load the new settings!"; else echo "Click only this to relaod the page.."; //display the cookie if set if(isset($_COOKIE['cookie_alive'])) { echo "

Well this is the cookie!
"; 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() { ?>
[{$str}][{$caller}]
"; $log="Log Begins:---".date('YMd 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(); ?>

识别屏幕后,您可以像这样出现:

在你的view.htm(无论如何)文件包含这样的样式:

   

那么你的style.php可能是这样的:

 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: height: padding:10px ; } etc etc this is how I do it so html remain clear, css clear, calculations separated. 

只是我的方式,这是没有必要的好!

嘿,我发现有一个与此相关的有用主题,

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