javascript - Read lines from a file and import as youtube embed to my site -
i want know how read text file , information javascript code. mean is,
if have text file contained youtube video-id , title
id:youtubevideoid1 "title1" id:youtubevideoid2 "title2" id:youtubevideoid3 "title3" id:youtubevideoid4 "title4"
for example: id:gsjtg7m1mmm "x-man trailer!"
i want web (index.html) read text-file's lines 1 1 ,by order, , show me on web embed of youtube videos this:
<a href="https://www.youtube.com/watch?v=(videoid text file)" title="(title text file)"><img width="143" alt="(title)" src="http://img.youtube.com/vi/(video_id)/hqdefault.jpg" ></a>
it need read , show lines start "id:" , first word after id video_id (so should var) , after "space" comes "title" (and can few words in language) should var also..
so if have 5 rows in text file id + title, show me on page, 5 embed videos... , if there more, more...
and if javascript, code need write on web show?
thank you! hope me that..
this seems relatively simple.
first off need read txt file:
function readtextfile(file) { var rawfile = new xmlhttprequest(); rawfile.open("get", file, false); rawfile.onreadystatechange = function () { if(rawfile.readystate === 4) { if(rawfile.status === 200 || rawfile.status == 0) { var alltext = rawfile.responsetext; return alltext; } } } rawfile.send(null); } var txt = readtextfile("path/to/txt/file.txt");
then need parse file, assuming file looks above this:
var split_txt = txt.split("id:"); for(var i=0;i<split_txt.length;i++){ var id = split_txt[i].split(" \"")[0]; var title = split_txt[i].split(" \"")[1].slice(0,-1); $("body").append("<a href=\"https://www.youtube.com/watch?v="+id+"\" title=\""+title+"\"><img width=\"143\" alt=\""+title+"\" src=\"http://img.youtube.com/vi/"+id+"/hqdefault.jpg\" ></a>"); }
you require jquery run it.
Comments
Post a Comment