最近要做一個微信內打開要跳轉到外部瀏覽器的東西,研究了下,搞了一個demo,分享一下。
一.瀏覽器跳轉微信
1.使用微信內置的url協議
<a href="weixin//"></a>
簡單方法,僅僅能打開微信app,並不能執行任何命令。
2.ticket
weixin://dl/business/?ticket=t12a18b6ac3a603bfae46bd17b58f1
ticke往往是和一些大公司合作,將地址轉化為ticket進行直接跳轉。好像還不能隨便使用
3.暫時攜帶url參數進行跳轉的還未知
二,微信打開外置瀏覽器(ios的還要研究)
<?php
error_reporting(0);
if($_GET['open']==1 && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!==false){
header("Content-Disposition: attachment; filename=\"load.doc\"");
header("Content-Type: application/vnd.ms-word;charset=utf-8");
}
?>
<script>
var url = ''; //定義url跳轉
window.mobileUtil = (function(win, doc) {
var UA = navigator.userAgent,
isAndroid = /android|adr/gi.test(UA),
isIOS = /iphone|ipod|ipad/gi.test(UA) && !isAndroid,
isBlackBerry = /BlackBerry/i.test(UA),
isWindowPhone = /IEMobile/i.test(UA),
isMobile = isAndroid || isIOS || isBlackBerry || isWindowPhone;
return {
isAndroid: isAndroid,
isIOS: isIOS,
isMobile: isMobile,
isWeixin: /MicroMessenger/gi.test(UA),
isQQ: /QQ/gi.test(UA)
};
})(window, document);
//微信環境判斷
if(mobileUtil.isWeixin){
//ios
if(mobileUtil.isIOS){
//解析地址未成功
window.location.replace(url);
}
//android
else if(mobileUtil.isAndroid){
url = '?open=1';//重新定義url地址
window.location.replace(url);
}
}
//其他設備
else{
//重載跳轉頁面
window.location.replace(url);
}
</script>
//上傳服務器,直接訪問文件就可以跳轉