前幾天寫移動端專案用alert和confirm進行資訊提示,但發現在iOS系統中,每次提示資訊上面都會被新增一行URL地址,安卓卻沒有,經過查詢之後,果然不出所料,相容!!相容!!!相容!!!
需要重新alert和confirm,不廢話,,看程式碼!
alert重寫
<script type="text/javascript"> var wAlert = window.alert; window.alert = function (message) { try { var iframe = document.createElement("IFRAME"); iframe.style.display = "none"; iframe.setAttribute("src", `data:text/plain,`); document.documentElement.appendChild(iframe); var alertFrame = window.frames[0]; var iwindow = alertFrame.window; if (iwindow == undefined) { iwindow = alertFrame.contentWindow; } iwindow.alert(message); iframe.parentNode.removeChild(iframe); } catch (exc) { return wAlert(message); } } </script>
confirm重寫
<script> var wConfirm = window.confirm; window.confirm = function (message) { try { var iframe = document.createElement("IFRAME"); iframe.style.display = "none"; iframe.setAttribute("src", `data:text/plain,`); document.documentElement.appendChild(iframe); var alertFrame = window.frames[0]; var iwindow = alertFrame.window; if (iwindow == undefined) { iwindow = alertFrame.contentWindow; } var result=iwindow.confirm(message); iframe.parentNode.removeChild(iframe); return result; } catch (exc) { return wConfirm(message); } } var r=confirm("Press a button"); if (r==true) { document.write("You pressed OK!"); } else { document.write("You pressed Cancel!"); } </script>
然後瞭解到html中data型別的url如下:
html中data型別的url
data:,<文字資料>
data:text/plain,<文字資料>
data:text/html,<HTML程式碼>
data:text/html;base64,<base64編碼的HTML程式碼>
data:text/css,<CSS程式碼>
data:text/css;base64,<base64編碼的CSS程式碼>
data:text/javascript,<Javascript程式碼>
data:text/javascript;base64,<base64編碼的Javascript程式碼>
data:image/gif;base64,base64編碼的gif圖片資料
data:image/png;base64,base64編碼的png圖片資料
data:image/jpeg;base64,base64編碼的jpeg圖片資料
data:image/x-icon;base64,base64編碼的icon圖片資料