简体   繁体   中英

Javascript not loading another page into DIV

I am having some troubles today. I am trying to get Javascript to load content into a <div> . Now, this is my JavaScript source code:

function loadXMLDoc(filename) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            after_load_function(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", filename, true);
    xmlhttp.send();
}

function after_load_function(responseText) {
    document.getElementById("right").innerHTML = responseText;
}

window.onload = function () {
    loadXMLDoc("welcome.html");
}

And my div:

<div id="right"></div>

Does anyone know the problem?

Is there any reason you aren't using an abstraction like the JQuery load method ?

$( document ).ready(function() {
    $("#right").load( "welcome.html" );
})

looks pretty good to me.

Just had a similar issue, here is what I have and it seems to be working really well,

HTML:

<html>
<head>
    <title> pageOne </title>
    <script src="jquery-1.11.1.min.js"></script>
</head>

<body>

    <input class='loadBtn' type='submit' value='Load a Different Page!:'>

    <div id="page"></div>

</body>
</html>

JS:

//click function:
$(".loadBtn").click(function(){

        //div to hide/replace
        $('#page')

            //fadeOut div content:
            .delay(200).fadeOut('slow')

            //load method:
            .load

            //url #divToReplace
            ('pageTwo.php #page')

            //fadeIn new div content:
            .hide().delay(300).fadeIn('slow');

        //prevent default browser behavior
        return false;
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM