cordova.js的坑

最近在使用cordova開發app,一開始只引用了cordova.js,cordova經過function exec(){}來連接本地和H5,以前代碼被下載到了本地,在蘋果和android上都能實現功能,但是有一天忽然要放到遠程上,原本覺得沒事,放就放唄,我H5寫的頁面還怕不成,但是問題來了,在android上能夠正常實現,但是在ios上只出來了靜態頁面,一點加載的東西都沒有.後來換了ios加載的cordova,這下在ios上沒事,在android什麼都沒有,這下明白了,敢情cordova.js不是通用的啊!還得先判斷機型,而後在引不一樣的js文件.方法以下:javascript

var head = document.getElementsByTagName('head')[0];html

var script = document.createElement('script');html5

var oScript = document.createElement('script');java

script.type = 'text/javascript';android

oScript.type = 'text/javascript';ios

script.src = 'http://xhtml5.duapp.com/cordova_plugins.js';app

var u = navigator.userAgent;spa

if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//android手機htm

    oScript.src = 'http://xhtml5.duapp.com/cordova.js';cordova

    head.appendChild(oScript);

    head.appendChild(script);

} else if (u.indexOf('iPhone') > -1) {//蘋果手機

    oScript.src = 'http://xhtml5.duapp.com/cordova_ios.js';

    head.appendChild(oScript);

}

;寫了這段代碼以後,再運行,果真在ios和android上都能實現功能!

相關文章
相關標籤/搜索