如何打开另一个文件index.html,具体取决于屏幕的宽度?

时间:2013-11-06 23:04:22

标签: javascript jquery html css

这是我的问题。

我已经完成了页面(html + css + js),但我想做一个移动版本,其他html文件,另一个index.html和css文件。我需要将代码添加到主页面,当我在屏幕宽度小于800像素的设备上打开网站时,它会启动移动版本吗?

3 个答案:

答案 0 :(得分:1)

这可能不是您想要的答案,但是如果用户使用触摸屏设备(即手机,平板电脑),此脚本将重定向用户

<script>
    if( 'ontouchstart' in window ){
        window.location = 'mobile/index.html';
    }
</script>

答案 1 :(得分:1)

将它放在你的onload中:

if(window.innerWidth < 800{
    window.location = '/m/index.html';
}

或(这是跨浏览器)

if(window.screen.width < 800{
    window.location = '/m/index.html';
}

答案 2 :(得分:0)

在子域或子文件夹上创建您的移动版本,并添加JavaScript移动用户检测并将其重定向到移动页面

https://github.com/sebarmeli/JS-Redirection-Mobile-Site