简体   繁体   中英

dynamically loading javascript fails, but innerHTML does

i've wrote a script in which javascript file and innerHTML refreshes time to time

setInterval(activeload,1000);

function activeload() {

    activediv.innerHTML="";
    scriptsrc.src="http://localhost/mypro/pro/activeuser.php";
    for(i=0;i<=activeuser.length;i++) {
        if(activeuser[i]!=null)
            activediv.innerHTML+="<p onclick='dial(' " + activeuser[i] +" ' )'> " + activeuser[i] +"</p><br>";
    }

    scriptsrc.src='';   
}

in the above script, innerHTML is modifying, but src attribute of script is not changing...
the js file loaded is

<script src="http://localhost/mypro/pro/activeuser.php" id="scriptsrc" type="application/javascript"></script>

this php file refreshes every 5 secs and is accurate in information.
need some help in loading the javascript perfectly

Although it's not clear to me what you want to do with the array that comes from activeuser.php , it seems like AJAX will be your best bet to bring it in to your page on a regular interval. Here is a basic example of an AJAX call using jQuery:

<script src="jquery-2.1.4.min.js"></script>
<script>

setInterval(function() {

    $.ajax("/mypro/pro/activeuser.php").done(function(data) {
        console.log(data);
    });

}, 5000);

</script>

The way it works is that the $.ajax() call will request activeuser.php from the server. As soon as the file is delivered, the anonymous function inside of .done() will be called. That function has one parameter, which I've named data , that contains the contents of activeuser.php .

AJAX is a very convenient way to request data from the server without reloading the entire current page when the data is delivered to the browser.

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