js重写alert confirm弹框

function initConfirmAlert() {
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        window.confirm = function (name) {
            var iframe = document.createElement("IFRAME");
            iframe.style.display = "none";
            iframe.setAttribute("src", 'data:text/plain,');
            document.documentElement.appendChild(iframe);
            var result = window.frames[0].window.confirm(name);
            iframe.parentNode.removeChild(iframe);
            return result;
        }
        window.alert = function (name) {
            var iframe = document.createElement("IFRAME");
            iframe.style.display = "none";
            iframe.setAttribute("src", 'data:,');
            document.documentElement.appendChild(iframe);
            window.frames[0].window.alert(name);
            iframe.parentNode.removeChild(iframe);
        }
    } else {
        window.confirm = function (name) {
            var iframe = document.createElement("IFRAME");
            iframe.style.display = "none";
            //iframe.setAttribute("src", 'data:text/plain,');
            document.documentElement.appendChild(iframe);
            var result = window.frames[0].window.confirm(name);
            iframe.parentNode.removeChild(iframe);
            return result;
        }
        window.alert = function (name) {
            var iframe = document.createElement("IFRAME");
            iframe.style.display = "none";
            //iframe.setAttribute("src", 'data:,');
            document.documentElement.appendChild(iframe);
            window.frames[0].window.alert(name);
            iframe.parentNode.removeChild(iframe);
        }
    }
}

调用方法:
$scope.submit = function () {
$scope.data = {
“userId”: params.userId || “”,
“Passport”: $scope.user.passport || “”,
“Tel”: $scope.user.phone || “”,
“name”: $scope.user.username || “”,
“GoodsId”: params.goodsId || “”,
“CouponId”: params.couponId|| null
};
$http.post(‘/api/Order/CreateJJCVisaOrder’, $scope.data)
.then(function (success) {
if(confirm(‘确认付款嘛?’)){
//checkedPay选择支付方式radio
try {
$window.location.href = “jgos://payment/“ + success.data.generalOrderId + “/“ + $scope.checkedPay;

              } catch (e) { }
          }

          },
        function (error) {
            console.log(error);
        });
};

热评文章