項目用的js框架是angularJS,遇到一個和瀏覽器兼容相關的bug,功能是按鈕點擊出現一個popupWindow,再點擊popupWindow右上角的關閉按鈕時,在Safari瀏覽器中會崩潰而Chrome以及Firefox瀏覽器中均能正常顯示。
報錯內容如下:
Safari can’t open “unsafe:javascript:void(0);” because macOS doesn’t recognize Internet addresses starting with “unsafe”
如下圖所示:
根據其錯誤信息可以大體推測出和javascript:void(0)
以及macOS不能識別有關,全局搜索javascript:void(0)
在項目中找到這樣寫的代碼,調試着修改。我的問題代碼是在這裏:
<a href="javascript:void(0)" data-ng-click="showPopup()" class="close-link"><img alt="close"></a>
展開popup後點擊關閉時,通過href
標籤指向本頁就實現了關閉的功能了,但是macOS不能識別,原因是angular對href有安全檢查,只能生成它認爲安全的鏈接,javascript不在angularJS的白名單中,將href="javascript:void(0)"
修改爲href="#"
即可,代碼如下:
<a href="#" data-ng-click="showPopup()" class="close-link"><img alt="close"></a>
還有一種修改方式是配置編譯:
angular.module('app').config( [
'$compileProvider',
function( $compileProvider )
{
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|sms):/);
// Angular v1.2 之前使用 $compileProvider.urlSanitizationWhitelist(...)
}
]);
以上即爲解決方式,如有疑問請及時指出,其中參考鏈接如下及大致概要如下;
1.https://gitter.im/720kb/angular-datepicker?at=5afe7357b84be71db9178260
Since javascript is not in the sanitization whitelist by default in angularjs, it will be replaced with href=”unsafe:javascript:void(0)”. If you click on a link like this, Safari will try to open a page and just shows an error called Safari can’t open “unsafe:javascript:void(0)” because macOS doesn’t recognize Internet addresses starting with “unsafe:”.
The javascript protocoll can be added to the whitelist to fix this:
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|s?ftp|mailto|tel|file|javascript):/);
However, i don’t think thats recommended. It would be better just to change the href to # or just simply remove it.
2.http://eyehere.net/2015/angularjs-unsafe/ 標題:“關於angularJs中的unsafe”
angular.module('app').config( [
'$compileProvider',
function( $compileProvider )
{
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|sms):/);
// Angular v1.2 之前使用 $compileProvider.urlSanitizationWhitelist(...)
}
]);