h5和app交互总结
- 与app约定跳转协议(jsbridge),h5向app原生跳转时则根据协议:
- window.location.href = 'xmj://login?token='+token
- app原生调用h5页面方法则需
- 将js方法暴露给原生,放在最外层,先声明
<script type="text/javascript"> var onResume = function(){} </script>
然后可以在自己的js继续完成方法
onResume = function(){ document.getElementsByClassName("login_btn")[0].style.display = 'block'; }
- app原生给h5传参
- app原生将参数存储在cookie中,
- h5给原生传参
- 放在url后边的参数中,
- xmj://share?params=XXX
-
其中XXX为经过BASE64 UTF-8 UrlSafe 编码的Json数据
-
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64encode(D) { var C, A, E; var G, F, B; E = D.length; A = 0; C = ""; while (A < E) { G = D.charCodeAt(A++) & 255; if (A == E) { C += base64EncodeChars.charAt(G >> 2); C += base64EncodeChars.charAt((G & 3) << 4); C += "=="; break } F = D.charCodeAt(A++); if (A == E) { C += base64EncodeChars.charAt(G >> 2); C += base64EncodeChars.charAt(((G & 3) << 4) | ((F & 240) >> 4)); C += base64EncodeChars.charAt((F & 15) << 2); C += "="; break } B = D.charCodeAt(A++); C += base64EncodeChars.charAt(G >> 2); C += base64EncodeChars.charAt(((G & 3) << 4) | ((F & 240) >> 4)); C += base64EncodeChars.charAt(((F & 15) << 2) | ((B & 192) >> 6)); C += base64EncodeChars.charAt(B & 63) } return C } function utf16to8(str) { var out, i, len, c; out = ""; len = str.length; for(i = 0; i < len; i++) { c = str.charCodeAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { out += str.charAt(i); } else if (c > 0x07FF) { out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } else { out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } } return out; }
如若喜欢,欢迎转载,请在文章页面明显位置给出此文链接!
若您觉得这篇文章还不错请点击下右下角的推荐,有了您的支持才能激发作者更大的写作热情,非常感谢!