Dynamically change the src of a script tag

前端 未结 1 1712
萌比男神i
萌比男神i 2021-01-22 16:52

I\'ve read some of the similar questions but they don\'t quite answer this query of mine.

I am building a website that uses Viddler video hosting. In their embed code yo

相关标签:
1条回答
  • 2021-01-22 17:22

    With jQuery you can load and execute JavaScript on demand. Before loading the script, append the playlist part to the script url like this:

    HTML

    <a href="playlisturl" class="playlist-link">Play list A</a>
    <a href="playlisturl" class="playlist-link">Play list B</a>
    <a href="playlisturl" class="playlist-link">Play list C</a>
    

    jQuery

    $(".playlist-link").click(function(e){
        e.preventDefault();
        var playlist = $(this).attr("href");
    
        var url = 'http://www.viddler.com/tools/vidget.js' +
                      '?widget_type=js_list' +
                      '&widget_width=940' +
                      '&source=playlist' +
                      '&user=USERNAME ' +
                      '&playlist='+ playlist +
                      '&style=grid'+
                      '&show_player=true'+
                      '&player_type=simple'+
                      '&max=12'+
                      '&videos_per_row=6'+
                      '&v=2.0'+
                      '&id=XXXXXXXXXX';
    
        $.getScript(url, function(data, textStatus){
            alert('This is executed after your script is loaded');
        });
    });
    
    0 讨论(0)
提交回复
热议问题