admin管理员组文章数量:1291656
I know it doesn't necessarily make any sense, but is there a way to run javascript on the client, find the screen width/height of the active browser, and then pass those variables to a PHP script?
All the mon knowledge is usually PHP >> Javascript, never trust the client I suppose, and I can't find a way at the moment.
This is for a .php file, so blocks easily, but I cannot seem to do the reverse.
I am working on creating a JSON object, but then realized I still have the same wall to climb.
Thanks.
I am finding the interactions between technologies somewhat challenging as an amateur, but it makes me feel powerful to know. Maybe soon I'll be able to stop exceptions just with my hands. Well, the matrix reference fails, because that is what people do on a second tier literal level.
I know it doesn't necessarily make any sense, but is there a way to run javascript on the client, find the screen width/height of the active browser, and then pass those variables to a PHP script?
All the mon knowledge is usually PHP >> Javascript, never trust the client I suppose, and I can't find a way at the moment.
This is for a .php file, so blocks easily, but I cannot seem to do the reverse.
I am working on creating a JSON object, but then realized I still have the same wall to climb.
Thanks.
I am finding the interactions between technologies somewhat challenging as an amateur, but it makes me feel powerful to know. Maybe soon I'll be able to stop exceptions just with my hands. Well, the matrix reference fails, because that is what people do on a second tier literal level.
Share Improve this question asked Apr 3, 2011 at 1:14 DanedoDanedo 2,2635 gold badges30 silver badges37 bronze badges 2- what do you want to acplish passing those variables? – amosrivera Commented Apr 3, 2011 at 1:17
- The php script is usually already terminated when the browser receives the answer so the only way is to use AJAX or an embedded file with the resolution info passed as parameters. Like a 1x1 tracking pixel. – Eliasdx Commented Apr 3, 2011 at 1:19
3 Answers
Reset to default 5You can do this:
<script>
document.getElementById('hidden-field').value = screen.width + ',' + screen.height;
</script>
<input id="hidden-field" name="dimensions" value="" />
and submit the form. You could use AJAX to send that data instead, a cookie, an image, etc. I personally would use AJAX.
But yes, this is a case where you'll need to just trust the client at face value. However, if you are doing something with that data (like creating an image or something) you should still validate that the value makes sense so you don't end up trying to create an image that is 100 million pixels wide, etc.
<script type="text/javascript">
document.write('<img src="index.php?width='+screen.width+'&height='+screen.height+'"/>');
</script>
(oh lookie here, no AJAX :))
By the way, I highly advice not to use document.write
, instead either use window.onload
or something better from your preferred JS library (such as jQuery(document).ready(function(){ jQuery(document.body).append("the-image-as-in-my-example"); })
in jQuery).
Fast one with jQuery
$(function(){
$.ajax({
url: 'file.php',
type: 'POST',
data: {h: screen.height, w: screen.width}
});
});
本文标签: Javascript screen widthheight to PHPStack Overflow
版权声明:本文标题:Javascript screen widthheight to PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741538048a2384148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论