在jsp中将屏幕宽度作为变量 [英] Getting screen width as a variable in jsp

查看:98
本文介绍了在jsp中将屏幕宽度作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道调整图像大小(它需要被传递为img src =blah.jpg?width = x),所以我的页面可以显示不同的大小,有人可以帮助我在这里?我只需要

I need to know to resize an image (it needs to be passes as img src="blah.jpg?width=x) so my page can be displayed at different sizes, can someone help me out here? I just need x (width) as a jsp variable.

推荐答案

你所要求的不能用这种方式。在服务器上呈现,并作为HTML发送到浏览器。只有当页面到达浏览器时,屏幕宽度可用(使用JavaScript),所以将其作为JSP变量是不可能的。

What you are asking for can't be done in this way. Your JSP is rendered on the server, and sent to the browser as HTML. Only once the page reaches the browser, is the screen width available (using JavaScript). So getting it as a JSP variable is simply not possible.

你可以使用JavaScript来获取上一页的屏幕宽度,并将其设置为链接中的查询参数,或者作为表单的隐藏字段。 ,或者提交表单,您可以访问请求以获取屏幕宽度。

What you could instead, is use JavaScript to get the screen width in the previous page, and set it as a query parameter in a link, or as a form's hidden field. When the link is clicked, or the form is submitted, you can access the request to get the screen width.

当然,如果用户可以访问

Of course, this doesn't work if it's possible for the user to access the URL for that page by entering it directly in the browser.

或者,使用JavaScript在加载网页后加载图片 此时您将拥有屏幕宽度,并可以相应地构建网址。基本上,你将使用主体的 onload 事件(或在JQuery中的 $(document).ready ),调用javascript,即将您的图片的 src 属性设置为您需要使用的网址。

Alternatively, use JavaScript to load the image after the page has loaded. You'd have the screen width at this time, and could construct the URL accordingly. Essentially, you'd use the onload event of the body (or $(document).ready in JQuery), to call javascript, that sets the src attribute of your image, to the URL that you need to use.

这篇关于在jsp中将屏幕宽度作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆