JavaScript函数调用在iOS上的Safari中无效

时间:2012-10-24 11:56:36

标签: javascript ios function safari

我使用此JavaScript代码......

function data_addproduct(id){
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "addproduct";
var vars = "data="+data+"&id="+id;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_checkout").innerHTML = return_data;
        document.getElementById("ui_checkout_mobile").innerHTML = return_data;
        data_refreshtotal();
        data_refreshproducts();
    }
}
hr.send(vars);
document.getElementById("ui_checkout").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_mobile").innerHTML = "<br>Updating...";

}

然而,在iOS上的Safari中,它没有调用其他两个函数......

data_refreshtotal();
data_refreshproducts();

奇怪,因为它在Chrome for iOS中运行良好,有人知道如何在Safari中修复它吗?

它调用的函数:

function data_refreshtotal(){   
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "refreshtotal";
var vars = "data="+data;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_total").innerHTML = return_data;
        document.getElementById("ui_total_mobile").innerHTML = return_data;
        document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
    }
}
hr.send(vars);
document.getElementById("ui_total").innerHTML = "Updating...";
document.getElementById("ui_total_mobile").innerHTML = "Updating...";
document.getElementById("ui_checkout_notify").innerHTML = "Updating...";
}
function data_refreshproducts(){    
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "refreshproducts";
var vars = "data="+data;

hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_checkout").innerHTML = return_data;
        document.getElementById("ui_checkout_mobile").innerHTML = return_data;
        document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
    }
}
hr.send(vars);
document.getElementById("ui_checkout").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_mobile").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_notify").innerHTML = "Updating...";
}

1 个答案:

答案 0 :(得分:0)

根据您发布的内容,唯一有意义的是(某种程度上)data_refreshtotaldata_refreshproducts在被调用时已超出范围。这是相同的代码,重构和命名空间,以确保在调用它时每个函数仍在范围内:

var Cart;
if (!Cart) {
    Cart = {};
}
(function () {
    'use strict';
    Cart.stringificate = function stringificate(data) {
        var key = '',
            postData = '';
        for (key in data) {
            if (data.hasOwnProperty(key)) {
                postData += '&' + key + '=' + data[key];
            }
        }
        return postData.substring(1);
    };
    Cart.ajax = function ajax(options) {
        var hr = new XMLHttpRequest(),
            method = options.method ? options.method.toUpperCase() : "POST",
            url = options.url || "scripts/data_processing.php",
            i = 0;
        hr.open(method, url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function () {
            if (hr.readyState === 4 && hr.status === 200) {
                options.callback(hr.responseText);
            }
        };
        hr.send(Cart.stringificate(options.data));
        for (i = 0; i < options.notices.length; i += 1) {
            document.getElementById(options.notices[i]).innerHTML = "<br />Updating&hellip;";
        }
    };
    Cart.data_refreshtotal = function data_refreshtotal() {
        Cart.ajax({
            "data": {
                "data": "refreshtotal"
            },
            "callback": function (return_data) {
                document.getElementById("ui_total").innerHTML = return_data;
                document.getElementById("ui_total_mobile").innerHTML = return_data;
                document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
            },
            "notices": [
                "ui_total",
                "ui_total_mobile",
                "ui_checkout_notify"
            ]
        });
    };
    Cart.data_refreshproducts = function data_refreshproducts() {
        Cart.ajax({
            "data": {
                "data": "refreshproducts"
            },
            "callback": function (return_data) {
                document.getElementById("ui_checkout").innerHTML = return_data;
                document.getElementById("ui_checkout_mobile").innerHTML = return_data;
                document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
            },
            "notices": [
                "ui_checkout",
                "ui_checkout_mobile",
                "ui_checkout_notify"
            ]
        });
    };
    Cart.data_addproduct = function data_addproduct(id) {
        Cart.ajax({
            "data": {
                "data": "addproduct",
                "id": id
            },
            "callback": function (return_data) {
                document.getElementById("ui_checkout").innerHTML = return_data;
                document.getElementById("ui_checkout_mobile").innerHTML = return_data;
                Cart.data_refreshtotal();
                Cart.data_refreshproducts();
            },
            "notices": [
                "ui_checkout",
                "ui_checkout_mobile"
            ]
        });
    };
}());

我使用Cart作为命名空间只是因为它好像你正在制作一个购物车(并且很快输入:))。尝试运行它,看看函数是否被调用。