user1460015
user1460015

Reputation: 2003

Displaying subtitles using javascript

I'm currently using jquery.srt.js to display subtitles for some videos that I have.

What I am trying to accomplish is displaying the whole text file of the subtitles in a webpage.

This is what I have in my html document. How can I get the 'data-srt' (a local file) contents?
Thanks

<div id='srt'
     class="srt"
     data-video="video"
     data-srt="sample.srt" />

</div>

EDIT

    $(function()
    {
        $.get($("#srt").data("srt"), function(text) {
            alert(text);
        });
    });

</script>

I tried the above editied version but instead of displaying the subtitle text it gives me:
[object XMLDocument]

Upvotes: 0

Views: 3513

Answers (2)

Joe Johnson
Joe Johnson

Reputation: 1824

I see you're using W3C DOM methods, but your file indicates you're using jQuery. So, the following is shorter using jQuery:

$.get($("#srt").data("srt"), function(text){
  alert(text); // Show the subtitles from the text file.
});

Enjoy!

Upvotes: 0

AceCorban
AceCorban

Reputation: 2103

I'm assuming sample.srt is a text file located on the webserver? You can try performing an ajax call to the text file and using javascript to display the response text to an html element. Use jQuery to perform that AJAX call:

$.ajax(
{
   url: $("#srt").attr("data-srt"),
   success: function(responseText)
   {
       alert(responseText);
   }
});

Upvotes: 1

Related Questions