<!doctype html> <html> <head> <meta charset="utf-8"/> <title>redirectToNative的demo</title> </head> <body> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <div style="font-size:16px;color:#333"> 若是沒有自動下載,請手動<a style="color:red;font-size:20px" href="http://m.etao.com/download.php?src=unsupport">點此下載一淘客戶端</a>,安裝後用一淘客戶端的掃描功能從新掃描二維碼便可;</br></br> 若是您不想安裝客戶端,也能夠<a style="color:red;font-size:20px" id="J_gotoH5" href="http://m.etao.com/#item/">直接訪問etao觸屏版</a>,一樣有手機專享價以及優惠券和返利! </div> <script> /** * @fileoverview * @author miaojing<miaojing@taobao.com> * @module redirectToNative 移動頁面中間跳轉頁面使用,打開native app,沒安裝則引導去下載app,不依賴任何kissy模塊 * @ps 線上運行數月至今,目前測試發現魅族手機沒法識別etao://item 懷疑手機系統不支持,具體緣由客戶端開發排查目前無果,這種狀況可在跳轉頁面添加文本再次引導用戶 **/ /** * @class RedirectToNative * @constructor */ var RedirectToNative = { /** * iosNativeUrl: string 必選 ios app上自定義的url scheme) 如 taobao://home(淘寶首頁) etao://item?nid=xxx(一淘商品詳情頁) * andriodNativeUrl: string 必選 android app自定義的url scheme * iosInstallUrl: string 必選 ios app store裏的安裝地址 * androidInstallUrl: string 必選 android app的apk地址 * package: string 可選 默認com.taobao.taobao android的包名,如淘寶爲com.taobao.taobao,etao爲com.taobao.etao * iosOpenTime: int 可選默認800ms, 啓動ios客戶端所需時間,通常ios平臺總體性能不錯,打開速度較快 * androidOpenTime: int 可選默認2000ms,啓動android客戶端所需時間,android系統性能良莠不齊所需啓動時間也不齊,和android客戶端自己啓動時間也有關,好比3.0版本啓動一淘客戶端就平均比淘寶客戶端要慢200ms */ init: function (config) { var self = this self.platform = self._UA() // pc下 什麼都不處理 pc訪問下可能href能夠連接去其餘地址 if (!self.platform) return if (self.platform == 'ios') { self.installUrl = config.iosInstallUrl self.nativeUrl = config.iosNativeUrl self.openTime = config.iosOpenTime || 800 } else { self.installUrl = config.androidInstallUrl self.nativeUrl = config.andriodNativeUrl self.openTime = config.androidOpenTime || 3000 self.package = config.package || 'com.taobao.taobao' } //只有android下的chrome要用intent協議喚起native if (self.platform != 'ios' && !!navigator.userAgent.match(/Chrome/i)) { self._hackChrome(); } else { alert("kk") self._gotoNative(); } }, /** * _hackChrome 只有android下的chrome要用intent協議喚起native * https://developers.google.com/chrome/mobile/docs/intents * @return {[type]} */ _hackChrome: function () { var self = this var startTime = Date.now() var paramUrlarr = self.nativeUrl.split('://'), scheme = paramUrlarr[0], schemeUrl = paramUrlarr[1] window.location = 'intent://' + schemeUrl + '#Intent;scheme=' + scheme + ';package=' + self.package + ';end' setTimeout(function () { self._gotoDownload(startTime) }, self.openTime) }, /** * [_gotoNative 跳轉至native,native超時打不開就去下載] * @return */ _gotoNative: function () { var u = navigator.userAgent; var self = this var startTime = Date.now(), doc = document, body = doc.body, iframe = doc.createElement('iframe') iframe.id = 'J_redirectNativeFrame' iframe.style.display = 'none'; if(u.indexOf('Safari') > -1){ window.location.href=self.nativeUrl } else { iframe.src = self.nativeUrl; } // //運行在head中 if (!body) { setTimeout(function () { doc.body.appendChild(iframe) }, 0) } else { body.appendChild(iframe) } setTimeout(function () { doc.body.removeChild(iframe) self._gotoDownload(startTime) /** * 測試時間設置小於800ms時,在android下的UC瀏覽器會打開native app時並下載apk, * 測試android+UC下打開native的時間最好大於800ms; */ }, self.openTime) }, /** * [_gotoInstall 去下載] * @param {[type]} startTime [開始時間] * @return */ _gotoDownload: function (startTime) { var self = this var endTime = Date.now() if (endTime - startTime < self.openTime + 500) { window.location = self.installUrl } }, /** * [_UA 檢測平臺] * @return string [ios|android| ] */ _UA: function () { var ua = navigator.userAgent // ios if (!!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) { return 'ios' } else if (!!ua.match(/Android/i)) { return 'android' } else { return '' } }, } //透傳參數 var searchStr = location.search, iosNativeUrl = 'taobao://detail.tmall.com/item.htm?id=543501628646', andriodNativeUrl = 'taobao://detail.tmall.com/item.htm?id=543501628646', downloadUrl = 'https://mpage.taobao.com/hd/download.html?spm=a21bo.50862.1997563273.1.28689e73fipyOa', configObj = { iosInstallUrl:"http://itunes.apple.com/app/id387682726?mt=8" , androidInstallUrl: "http://download.alicdn.com/wireless/taobao4android/latest/702757.apk", iosNativeUrl: iosNativeUrl, andriodNativeUrl: andriodNativeUrl, package: 'com.taobao.taobao', } RedirectToNative.init(configObj) </script> </body> </html>