Extract ajax HTML response only last 50 lines

74 Views Asked by At

I am using an Ajax request to get the content of an HTML file. However, I would only like to use the last 50 lines of this HTML file.

The content of my HTML file looks like this:

<div class='userMessage'>01-12-2014 at 07:37 PM - <b>User</b>: message<br></div>
<div class='userMessage'>01-12-2014 at 07:38 PM - <b>User</b>: message<br></div>

The lines are all seperated with an end line: \n

My Ajax request looks like this:

$.ajax({
                url: "file.html",
                cache: false,
                success: function(html){    
                    $("#div").html(decodeHTMLEntities(html));
                }
});

Any help would be highly appreciated.

1

There are 1 best solutions below

0
On

Please use below code. Last fifty lines would be printed from last record.

        $.ajax({
            url: "server.php",
            cache: false,
            success: function(response_data) {
                var splitted_data = response_data.split("\n");

                var splitted_data_length = splitted_data.length;
                var splitted_data_min = splitted_data_length - 50;
                for (var i = splitted_data_length; i > splitted_data_min; i--) {
                    $("#div").append(splitted_data[i]);
                }                      
            }
        });
    });