简体   繁体   English

根据浏览器分辨率重定向URL

[英]Redirect URL Based On Browser Resolution

If user uses resolution smaller than 1024x600, i want it ro redirect them to the mobile version of my site. 如果用户使用的分辨率小于1024x600,我希望将其重定向到我的网站的移动版本。 Now i'm doing so using JQuery, if the resolution is less than 1024x600, it will be redirected to mobile version. 现在,我使用JQuery进行操作,如果分辨率小于1024x600,它将被重定向到移动版本。 I'm concerned whether it is a good practice or not (redirecting using script), and if it's now what do you suggest (since as far as i know PHP can't detect browser resolution)? 我担心这是否是一个好习惯(使用脚本重定向),如果现在是您的建议(因为据我所知PHP无法检测浏览器分辨率)?

Any suggestion would be appreciated, thank you. 任何建议将不胜感激,谢谢。

PS. PS。 I'm using a full-height website which looks nice in resolution 1024x600 or larger, but not on smaller ones, so i made the mobile version to go with full height website and mobile support. 我使用的是全高网站,分辨率为1024x600或更大,看上去不错,但是在较小的网站上却没有,因此我制作了移动版本,以配合全高网站和移动支持。 I don't want to use browser detection since i want mobile devices with big resolutions to access standard website. 我不想使用浏览器检测,因为我希望具有高分辨率的移动设备可以访问标准网站。

Edit: I was handed down a design of full-height single-page horizontally site, and i was having difficulties in making it responsive(if the content is fluid then it won't be full-height with no vertical scrolling) so i made a mobile version. 编辑:我被传授了一个全高单页水平站点的设计,并且我很难使其具有响应性(如果内容是流畅的,那么它就不会在没有垂直滚动的情况下成为全高)移动版本。

Its absolutely fine to redirect to another page depending on certain circumstances. 根据某些情况重定向到另一个页面是绝对好的。 But there are some other better ways of doing that. 但是还有其他更好的方法可以做到这一点。

To solve your problem, learn about types of layouts . 要解决您的问题,请了解布局类型

Fluid layout would suit best for your requirement. 流体布局将最适合您的要求。 Ideally you should be using same set of stylesheets for various viewports. 理想情况下,您应该为各种视口使用同一套样式表。

Take a look at jQuery mobile 's website. 看一下jQuery mobile的网站。 Try changing your browser window size and see how the content automatically "fits" into your window. 尝试更改浏览器窗口的大小,然后查看内容如何自动“适合”您的窗口。 You have to do the same thing. 您必须做同样的事情。 :) :)

Its a fluid layout. 它的布局很流畅。 :) :)

PS : I'd recommend not using CSS media queries. PS:我建议不要使用CSS媒体查询。 Of course they will make your life a lot easier, but if you care to REALLY improve your skills as long as CSS goes, try building your things with minimum use of advanced features. 当然,它们将使您的生活更加轻松,但是,只要您愿意,只要CSS能够真正提高您的技能,就可以尝试使用最少的高级功能来构建自己的东西。

Probably the best practice is to use CSS media queries to serve different CSS based on the user's screen dimensions, unless your mobile and desktop site are radically different. 最佳实践可能是使用CSS媒体查询根据用户的屏幕尺寸提供不同的CSS,除非您的移动网站和桌面网站完全不同。

You're right that PHP can not determine the end user's screen dimensions. 没错,PHP无法确定最终用户的屏幕尺寸。

The cleanest design is where you source one set of HTML/JS/CSS from the server and the page adapts itself to the available screen size using some combination of CSS media queries and/or javascript. 最干净的设计是从服务器上获取一组HTML / JS / CSS,并且页面使用CSS媒体查询和/或javascript的某种组合使其自身适应可用的屏幕尺寸。

If you can't do that or if you have other reasons to want to source different HTML from the server, then it's best to find a way to avoid client-side redirects all the time because those extra round-trips are undesirable (especially on mobile). 如果您无法执行此操作,或者您有其他原因想要从服务器中获取不同的HTML,则最好一直寻找一种避免客户端重定向的方法,因为这些额外的往返操作是不可取的(尤其是在移动)。

Some sites set a cookie in the browser that has the screen resolution in it and except for the first visit to the site, the server can then see the screen resolution in the cookie before it renders the site and you can avoid the client-side redirect and can render the desired page based on the resolution in the cookie. 某些站点在浏览器中设置了具有屏幕分辨率的cookie,并且除了第一次访问该站点外,服务器可以在呈现站点之前在cookie中查看屏幕分辨率,并且可以避免客户端重定向并可以根据Cookie中的分辨率呈现所需的页面。

For the first visit (when the cookie wasn't set yet), you do the client-side redirect and set the cookie. 对于第一次访问(尚未设置cookie的情况),您需要进行客户端重定向并设置cookie。

Then, you also have to modify that cookie if the user changes their screen resolution (which doesn't happen all that often). 然后,如果用户更改其屏幕分辨率(这种情况并非经常发生),则还必须修改该cookie。

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

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