开发者

AJAX function parameter is null?

i got a problem with JS开发者_运维问答:

On line 1 to 4 I take all "a"-Elements from the DOM and get their hrefs. later I want to reload the URL via AJAX, but the href does not arrive correctly... Whats wrong?

$(document).ready(function(){
    $('a').click(function(e){
       ajaxReload($(this).attr('href'));
       e.preventDefault();
    });
});

function ajaxReload(href) {

var xmlhttp = null;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", href, true);
    xmlhttp.onreadystatechange = function() {

        if(xmlhttp.readyState != 4) {
            document.write('loading');
        }
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert('hello');
            //alert('getting '+xmlhttp.status+' for '+href);


            var pureHTML = xmlhttp.responseText;
            var ajaxstart = pureHTML.indexOf('<!-- AJAX START -->');
            var ajaxend = pureHTML.indexOf('<!-- AJAX END -->');
            var ajaxContent = pureHTML.substring(ajaxstart, ajaxend);

            var writeContent = document.getElementById('content');
            writeContent.innerHTML = ajaxContent;

        }
    }
    xmlhttp.send(null);
}


Sorry if I've misunderstood your code. I think that you simply need to (at least approximately) just remove code as commented below:

//$('a').click = function(href) {

    var pureHTML = xmlhttp.responseText;
    var ajaxstart = pureHTML.indexOf('<!-- AJAX START -->');
    var ajaxend = pureHTML.indexOf('<!-- AJAX END -->');
    var ajaxContent = pureHTML.substring(ajaxstart, ajaxend);

    $("content").html(ajaxContent);

//  ajaxReload(href); //this would cause a loop?

//  return false;
//}

To answer your later question - you can change your event propagation handling to:

$(document).ready(function(){
    $('a').click(function(e){
       ajaxReload($(this).attr('href'));
       e.preventDefault();
    });
});

And for the further comments, maybe try changing your:

document.write('loading');

To:

$("content").html(xmlhttp.status); //so now you can see the loading status for testing
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜