简体   繁体   English

使用浏览器宽度而不是屏幕宽度重定向到移动设备

[英]redirect to mobile using browser width instead of screen width

Im looking to redirect my site to a mobile version by detecting the browser width as oppose to screen.width , is this possible? 我正在通过检测与screen.width相反的浏览器宽度来将网站重定向到移动版本,这可能吗? Here is the code I want to change... 这是我要更改的代码...

<script type="text/javascript">


<!--
if (screen.width <= 767) {
    window.location = "http://m.mysite.com";
}
//-->
</script>

screen.width doesnt seem to give a reliable response as the resolution is so high on mobiles (I think this is the issue). 由于手机分辨率很高, screen.width似乎没有给出可靠的响应(我认为这是问题所在)。 so then when I set the figure higher it starts loading up the mobile site on Ipads or small desktop browsers. 因此,当我将数字设置得更高时,它开始在Ipad或小型桌面浏览器上加载移动网站。

Id rather not go down the user agent route due as I dont want to have to be checking every site every time a new handset gets released. 我宁愿不走用户代理路线,因为我不想每次发布新手机时都必须检查每个站点。

Thank you. 谢谢。

I'm not very familiar with this problem, but to help you, I googled a little bit around, I found some articles which may be of use to you, they include PHP, Javascript and some documentation about how & why. 我对这个问题不是很熟悉,但是为了帮助您,我在Google上搜索了一下,发现了一些可能对您有用的文章,其中包括PHP,Javascript以及有关如何及为什么的文档。

Maybe you'll find it usefull. 也许您会发现它很有用。

1 : 1:

http://twoninewebdesign.com/2012/01/Admin/mobile-device-redirect-for-your-website/ http://twoninewebdesign.com/2012/01/Admin/mobile-device-redirect-for-your-website/

2 : 2:

http://mobithinking.com/guide-device-detection http://mobithinking.com/guide-device-detection

3 : 3:

http://www.webhostinghub.com/support/website/how-to/mobile-redirect http://www.webhostinghub.com/support/website/how-to/mobile-redirect

Sincerly, Harmen Brinkman. 真诚的,哈曼·布林克曼。

I decided to go for a combination of user agent, screen width and window width. 我决定结合使用用户代理,屏幕宽度和窗口宽度。 This should make it as accurate as possible. 这应该使其尽可能准确。

I used the PHP code from here... http://detectmobilebrowsers.com/ And edited my javascript a little.. 我从这里使用了PHP代码... http://detectmobilebrowsers.com/并编辑了一些JavaScript。

<script type="text/javascript">

windowWidth = window.innerWidth;
<!--

if (screen.width <= 767 || windowWidth <= 767) {
    window.location = "http://m.mysite.com";
}
//-->
</script>
<?php //CODE FROM LINK ABOVE ?>

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

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