Javascript 屏幕宽度/高度到 PHP

发布于 2024-10-29 18:41:58 字数 344 浏览 1 评论 0原文

我知道这不一定有任何意义,但是有没有办法在客户端上运行 javascript,找到活动浏览器的屏幕宽度/高度,然后将这些变量传递给 PHP 脚本?

所有的常识通常就是PHP >> Javascript,我想永远不要相信客户端,而且我目前找不到方法。

这是针对 .php 文件的,因此很容易阻塞,但我似乎无法执行相反的操作。

我正在努力创建一个 JSON 对象,但后来意识到我仍然需要攀爬同一堵墙。

谢谢。

作为一名业余爱好者,我发现技术之间的相互作用有些挑战性,但这让我感到有力量去了解。也许很快我就能用手来阻止异常。好吧,矩阵引用失败了,因为这是人们在第二层文字级别上所做的事情。

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 common 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-11-05 18:41:58

您可以执行以下操作:

<script>
document.getElementById('hidden-field').value = screen.width + ',' + screen.height;
</script>

<input id="hidden-field" name="dimensions" value="" />

并提交表格。您可以使用 AJAX 来发送数据、cookie、图像等。我个人会使用 AJAX。

但是,是的,在这种情况下,您只需要相信客户的表面价值。但是,如果您正在使用该数据执行某些操作(例如创建图像或其他操作),您仍然应该验证该值是否有意义,这样您就不会最终尝试创建 1 亿像素宽的图像等。

You 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.

递刀给你 2024-11-05 18:41:58
<script type="text/javascript">
    document.write('<img src="index.php?width='+screen.width+'&height='+screen.height+'"/>');
</script>

(哦,看这里,没有 AJAX :)

顺便说一句,我强烈建议不要使用 document.write,而是使用 window.onload 或您首选的 JS 库中更好的东西(例如 jQuery(document).ready(function(){ jQuery(document.body).append("the-image-as-in -my-example"); }) 在 jQuery 中)。

<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).

烟酉 2024-11-05 18:41:58

使用 jQuery 快速实现

$(function(){
 $.ajax({
   url: 'file.php',
   type: 'POST',
   data: {h: screen.height, w: screen.width}
 });
});

Fast one with jQuery

$(function(){
 $.ajax({
   url: 'file.php',
   type: 'POST',
   data: {h: screen.height, w: screen.width}
 });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文