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

How can I open another file index.html, depending on the width of the screen?

本文关键字:取决于 屏幕 另一个 何打开 文件 索引 html      更新时间:2023-09-26

>这是我的问题。

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

把它放在你的负载中:

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

或(这是跨浏览器)

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

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

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

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

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