简体   繁体   中英

AJAX and JavaScript script loader

I would like to know how to load JavaScript file after loading of page via AJAX? The problem is: I have music player which uses JS file whith infomation about tracks. After that I load page all works nice. But when I load another page via AJAX my script doesn't work at page which I called.

Code looks like this:

function showContent(link,link2) {
    var cont = document.getElementById('content');
    var loading = document.getElementById('loading');
    window.history.replaceState('', 'Title', link2);
    var http = createRequestObject();                   
    if( http ) {
        http.open('get', link);                         
        http.onreadystatechange = function () {         

            if(http.readyState == 4) {

                cont.innerHTML = http.responseText;     
            }
        }
        http.send(null);    
    } else {
        document.location = link;   
    }
}
function createRequestObject() {
    try { return new XMLHttpRequest() }
    catch(e) {
        try { return new ActiveXObject('Msxml2.XMLHTTP') }
        catch(e) {
            try { return new ActiveXObject('Microsoft.XMLHTTP') }
            catch(e) { return null; }
        }
    }
}

Then I use angular-soundmanager2.js to make player alive. And this cut of code won't work at page which I load after linking to this page

 <h5>Songs</h5>
            <ul>
                <li ng-repeat="song in songs">

                    <button music-player="play" add-song="song">{{ song.title }}</button>
                    <button music-player add-song="song">+</button>
                </li>
            </ul>

            <button play-all="songs">Play all</button>
            <button play-all="songs" data-play="false">Add all</button>

It has to show me playlist. But I get nothing.

You must be binding the DOM element with javascript. On document ready you call something like this, $('#someDiv').makePlayer(). So, page loads it works fine. But when you make ajax, I guess you are replacing that page content on ajax. If you are removing and loading the DOM element again on which you called the makeplayer() earlier. You have to call makePlayer thing again after your ajax call.

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