1. 切换.html(同一套css)
网页通常需要适配pc端和移动端,适配可以通过响应式布局判断屏幕尺寸来展示不同的效果,还可以通过编写两个页面,也就是pc端和移动端分别写一个页面,这种方式更方便功能模块的编写。
通常情况下,pc端域名使用www,而移动端域名使用m,在编写pc和移动两个页面时,在实际页面加载时需要加载到对应的页面,所以需要判断设备的类型,让搜索引擎和浏览器知道,pc端用户应该访问那个页面,移动端用户应该访问哪个页面。
//判断是手机还是电脑
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
window.location.href = "./mobile/index.html"; //手机
} else {
window.location.href = "./web/index.html"; //电脑
}
2. 切换css(同一套html)
// 利用媒体查询 link不同的CSS文件:
<link rel="stylesheet" media="screen and (min-width:1px) and (max-width:500px)" href="/css/d1.css" />
<link rel="stylesheet" media="screen and (min-width:500px)" href="/css/d2.css" />
// meta标签:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />