叨校长灬 2019-07-01
浏览器打开本地文件,就是通过使用file协议
hybrid优点:
hybrid缺点:
适用场景
网上搜的微信部分的schema协议
weixin://dl/scan 扫一扫<!--以下是演示,无法正常运行,微信有严格的权限验证,外部页面不能随意使用schema--> function invokeScan() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = 'weixin://dl/scan'; // iframe 访问 schema var body = document.body || document.getElementByName('body')[0]; body.appendChild(iframe); setTimeout(function(){ body.removeChild(iframe); // 销毁iframe iframe = null; }); } document.getElementById('btn').addEventListener('click', function(){ invokeScan(); // html调用schema协议 }) // 如果要加上参数和callback,那么就要这么写 window['_weixin_scan_callback'] = function(result) { alert(result); } // ...省略... iframe.src = 'weixin://dl/scan?k1=v1&k2=v2&callback=_weixin_scan_callback'; // ...省略...
voke.js
(function (window, undefined) { // 封装schema function _invoke(action, data, callback){ // 拼装schema协议 var schema = 'myapp://utils/' + action; // 拼装参数 schema += '?a=a'; for(key in data){ if(data.hasOwnProperty(key)){ schema += '&' + key + '=' + data[key]; } } // 拼装callback callbackName = ''; if(typeof callback === 'string'){ callbackName = callback; } else { callbackName = action + Date.now(); window[callbackName] = callback; } schema += 'callback=callbackName'; // 触发schema var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = schema; var body = document.body; body.appendChild(iframe); setTimeout(() => { body.removeChild(iframe); iframe = null; }); } window.invoke = { share: function (data, callback) { _invoke('share', data, callback); } } })(window);
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button id="btn">分享</button> </body> <script src="voke.js"></script> <script> document.getElementById('btn').addEventListener('click',function(){ window.invoke.share({ titile:'111', content:'2222' }, function(res){ if (res.status){ alert('分享成功!'); } else { alert(res.message); } }); }); </script> </html>