-

电脑端与手机端界面互相切换JS和ASP和PHP来实现

电脑端与手机端界面互相切换JS和ASP和PHP来实现

JS版来实现PC端与手机端切换
案例:JS版实现预览>>


电脑端代码
$(function() {
    // alert("aaaa") 
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;
    // alert(userAgent)
    // 简单的移动设备检测  
    if (/Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent)) {
        // 重定向到mobile.html  
        window.location.href = 'mobile.html';
    }
});

var link = window.location ? window.location.href : window.location;
var ele = document.querySelector('.y-qrcode');
link += "mobile.html"
var qrcode = new QRCode(ele, {
    width: 200,
    height: 200
});
qrcode.makeCode(link);


手机端代码
function isInIframe() {  
    try {  
        return window.self !== window.top;  
    } catch (e) {  
        // 在某些情况下(如跨域iframe),尝试访问window.top可能会抛出安全异常  
        // 在这种情况下,我们可以假设它是在iframe中,但出于安全考虑,我们不能确定  
        return true; // 或者你可以选择返回false,具体取决于你的需求  
    }  
}  

$(function(){  

    if (isInIframe()) {  
        console.log('当前网页在iframe中');   // 在iframe里不处理
        // parent.location.href = 'default.html'; 
    } else {  
        var w=$(window).width() 
        if(w>600){ 
            console.log('当前网页不在iframe中');  
            window.location.href = 'default.html'; 
        }
    }


});  

        



ASP版来实现PC端与手机端切换
案例:ASP版实现预览>>


电脑端代码
'检测是否为手机浏览  '用这种20190420通用 
Function checkMobile()
    dim splstr,s,sagent
    checkMobile = False 
    sagent = lcase(Request.ServerVariables("HTTP_USER_AGENT")) & "" 
    splstr=split("mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile|ipod|iphone|android|opera mini|blackberry|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile","|")
    for each s in splstr
        if s<>"" then
            if instr(sagent,s)>0 then
                checkMobile = true 
                exit function
            end if
        end if
    next 
    If Request.ServerVariables("HTTP_X_WAP_PROFILE") <> "" Then     '这是旧版的
        checkMobile = True 
    End If 
End Function
if checkMobile() then
  response.redirect("mobile.asp")
  response.end()
end if


手机端代码
'检测是否为手机浏览  '用这种20190420通用 
Function checkMobile()
    dim splstr,s,sagent
    checkMobile = False 
    sagent = lcase(Request.ServerVariables("HTTP_USER_AGENT")) & "" 
    splstr=split("mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile|ipod|iphone|android|opera mini|blackberry|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile","|")
    for each s in splstr
        if s<>"" then
            if instr(sagent,s)>0 then
                checkMobile = true 
                exit function
            end if
        end if
    next 
    If Request.ServerVariables("HTTP_X_WAP_PROFILE") <> "" Then     '这是旧版的
        checkMobile = True 
    End If 
End Function
if request("check")<>"false" then
  if checkMobile()=false then
    response.redirect("default.asp")
    response.end()
  end if
end if  

        



PHP版来实现PC端与手机端切换
案例:PHP版实现预览>>


电脑端代码
function detectDevice() {  
    // 获取用户代理字符串  
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);  
  
    // 列出一些常见的手机和平板设备的用户代理字符串片段  
    $mobileAgents = array(  
        'mobile', 'android', 'silk/', 'kindle', 'blackberry', 'opera mini',  
        'opera mobi', 'windows phone', 'iemobile', 'iphone', 'ipod', 'ipad',  
        'mac os x', 'touch', 'mobile safari', 'android chrome', 'chrome/android',  
        'mobile safari/', 'fennec/', 'mozilla/.*firefox.*mobile'  
    );  
  
    // 检查用户代理字符串是否包含上述任何一个片段  
    foreach ($mobileAgents as $device) {  
        if (strpos($userAgent, $device) !== false) {  
            return 'mobile'; // 是手机端  
        }  
    }  
  
    // 如果不是上述任何一个移动设备,则默认为PC端  
    return 'pc';  
}  
// 调用函数检测设备类型  
$device = detectDevice();  
  
// 根据设备类型进行页面跳转  
if ($device === 'mobile') {  
    // 如果是手机端,则跳转到mobile.php  
    header('Location: mobile.php');  
    exit; // 确保脚本在发送重定向头后停止执行  
}


手机端代码
function detectDevice() {  
    // 获取用户代理字符串  
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);  
  
    // 列出一些常见的手机和平板设备的用户代理字符串片段  
    $mobileAgents = array(  
        'mobile', 'android', 'silk/', 'kindle', 'blackberry', 'opera mini',  
        'opera mobi', 'windows phone', 'iemobile', 'iphone', 'ipod', 'ipad',  
        'mac os x', 'touch', 'mobile safari', 'android chrome', 'chrome/android',  
        'mobile safari/', 'fennec/', 'mozilla/.*firefox.*mobile'  
    );  
  
    // 检查用户代理字符串是否包含上述任何一个片段  
    foreach ($mobileAgents as $device) {  
        if (strpos($userAgent, $device) !== false) {  
            return 'mobile'; // 是手机端  
        }  
    }  
  
    // 如果不是上述任何一个移动设备,则默认为PC端  
    return 'pc';  
}  
if ($_REQUEST['check'] !== "false") {  
    // 调用函数检测设备类型  
    $device = detectDevice(); 
    // 根据设备类型进行页面跳转  
    if ($device === 'pc') {  
        // 如果是手机端,则跳转到mobile.php  
        header('Location: default.php');  
        exit; // 确保脚本在发送重定向头后停止执行  
    }  
}