遇到的情景是这样的:
公司的APP需要增加一个新功能,可以在内嵌的页面上点击按钮跳转到相应的APP原生列表界面,内嵌的网页由后台编辑,但是以前的老版本APP没有这个功能,如果在页面上直接显示按钮,新版本没问题,老版本就会出现没反应的情况,因为老版本没有内嵌那个方法,所以如何兼容新老版本,我们得出一个简单的方法。
就是先获取APP的一些信息,新版本会增加一些版本的信息在里面,然后判断这个信息版本是不是大于某个版本值,如果是,那就显示新按钮,如果不是,那就显示的按钮跳转到wap页面。
[cce]
<script type="text/javascript">
//机型和版本判断
function CanShare() {
var ua = navigator.userAgent;
// 判断是否 iPhone 或者 iPod
if((navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i))) {
// 判断APP版本号是否大于630
if (/podinn\/(\S+)/i.test(ua)) {
var ver = RegExp['$1'];
if(parseFloat(ver) >= 630)
return true;
}
return false;
} else {
//安卓系统
if (/podinn\/(\S+)/i.test(ua)) {
var ver = RegExp['$1'];
if(parseFloat(ver) >= 630)
return true;
}
return false;
}
}
</script>
[/cce]
[cce]
<div id="divbutton1"><!--老版本显示部分-->
<a href="#">杭州</button></a>
</div>
<div id="divbutton2" style="display:none"><!--新版本显示部分-->
<a href="#">杭州</button></a>
</div>
<!--判断部分显示-->
<script>
$(function()
{
if(CanShare())
{
$("#divbutton1").hide();
$("#divbutton2").show();
}
});
</script>
[/cce]
APP信息获取概况
[cce]
<script type="text/javascript">
var x = navigator;
document.write("CodeName=" + x.appCodeName);
document.write("<br />");
document.write("MinorVersion=" + x.appMinorVersion);
document.write("<br />");
document.write("Name=" + x.appName);
document.write("<br />");
document.write("Version=" + x.appVersion);
document.write("<br />");
document.write("CookieEnabled=" + x.cookieEnabled);
document.write("<br />");
document.write("CPUClass=" + x.cpuClass);
document.write("<br />");
document.write("OnLine=" + x.onLine);
document.write("<br />");
document.write("Platform=" + x.platform);
document.write("<br />");
document.write("UA=" + x.userAgent);
document.write("<br />");
document.write("BrowserLanguage=" + x.browserLanguage);
document.write("<br />");
document.write("SystemLanguage=" + x.systemLanguage);
document.write("<br />");
document.write("UserLanguage=" + x.userLanguage);
</script>
[/cce]