要在index.html里面加上什么代码能实现手机和电脑打开同一网址显示不...
发布网友
发布时间:2022-05-01 10:59
我来回答
共2个回答
热心网友
时间:2022-04-20 17:12
实现手机和电脑打开同一网址显示不同网页,这需要判断浏览器是手机端还是电脑端,进而跳转不同的网页
运用JavaScript判断
//适配代码
<script type="text/javascript">
var browser = {
versions : function() {
var u = navigator.userAgent, app = navigator.appVersion;
return {//移动终端浏览器版本信息
trident : u.indexOf('Trident') > -1, //IE内核
presto : u.indexOf('Presto') > -1, //opera内核
webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile : !!u.match(/AppleWebKit.*Mobile.*/)
|| !!u.match(/AppleWebKit/), //是否为移动终端
ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp : u.indexOf('Safari') == -1,
//是否web应该程序,没有头部与底部
google:u.indexOf('Chrome')>-1
};
}(),
language : (navigator.browserLanguage || navigator.language).toLowerCase()
}
if(browser.versions.mobile){
//为移动端访问网页,跳转到移动端链接地址
window.location.href="移动端链接地址";
}else{
//为电脑端访问网页,跳转到电脑端链接地址
window.location.href="电脑端链接地址";
}
</script>
热心网友
时间:2022-04-20 18:30
<SCRIPT LANGUAGE="JavaScript">
function mobile_device_detect(url)
{
var thisOS=navigator.platform;
var os=new
Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows
Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows
CE","BlackBerry","IEMobile");
for(var i=0;i<os.length;i++)
{
if(thisOS.match(os[i]))
{
window.location=url;
}
}
</sprict>
把url换成你手机的网址就可以了
追问我把url替换成网址以后,提示Array那一行包含语法错误,是什么意思?
追答Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","WindowsPhone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
因为代码里包含回车和换行了