HTML读取javascript并将结果放入div

时间:2014-05-23 15:24:50

标签: javascript jquery html spotify

我是HTML / javascript的新手,我想制作显示Last.FM当前播放歌曲的内容,将其显示为html中的div,以文本形式显示,我有一个代码通过聊天发送当前歌曲在www.irccloud.com上,我想知道如果你可以改变它以便它可以被接收并放入页面上的DIV,代码如下:

和var r是完成的代码,所以我如何在div中执行某些操作,将上传链接作为源,然后从中获取var?如果是这样,我该怎么做?

我尝试过以下代码here

对不起,如果我没有意义。

(function () {
            var e = "DeviousRunner";
            window.lfmRecentTrack = function (t) {
                var n = (new Array).concat(t.recenttracks.track)[0];
                    var album, spurl;
                    if (n.album["#text"]) {
                        album = " (from " + n.album["#text"] + ")";
                    } else {
                        album = "";
                    }
                    try {
                        var spotify = new XMLHttpRequest();
                        spotify.open("GET", "https://ws.spotify.com/search/1/track.json?q=" + encodeURIComponent(n.artist["#text"] + " - " + n.name), false);
                        spotify.send();
                        var spotresp = JSON.parse(spotify.responseText);
                        if (spotresp["tracks"].length > 0) {
                            //var urisplit = spotresp["tracks"][0]["href"].split(":");
                            //spurl = " https://open.spotify.com/" + urisplit[1] + "/" + urisplit[2];
                            spurl = spotresp["tracks"][0]["href"];
                        } else {
                            console.log("spotify: couldn't get url");
                            spurl = "";
                        }
                    } catch(e) {
                        console.log("spotify: " + e.message);
                        spurl = "";
                    }
                    var r = "is listening to " + n.name + " by " + n.artist["#text"] + " " + album + " (" + spurl + ")";
            }
            var n = document.createElement("script");
            n.setAttribute("type", "text/javascript");
            n.setAttribute("src", "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + e + "&api_key=dd5fb083b94a7196cf696b9d7d11bc63&limit=1&format=json&callback=window.lfmRecentTrack");
            document.body.appendChild(n)
        })();

1 个答案:

答案 0 :(得分:0)

我更新了您的 FIDDLE
通过这个:

var element = document.getElementById("rss");
    element.innerHTML = r;

在函数内...

希望这对你有用