Generate Random Quotes from a Text File

So to do that, you need a text file full of quotes. HAHAHA
Separated by new line.

$('#btn_modal').click(function () {
    var num = 0;
    $.ajax({
        url: "https://dl.dropboxusercontent.com/u/75734877/taglines.txt",
        type: "GET",
        dataType: "text",
        beforeSend: function () {
            $('#kowts').show().html('<img src="http://www.feelol.com/Content/images/Loading.gif" />');
        },
        success: function (t) {
            var lines = t;
            var lineArr = lines.split('\n');
            num = Math.floor(Math.random() * lineArr.length);
            $('#kowts').show().text(lineArr[num]);
        },
        error: function(){
            alert('Error')
        }
    });
});

Split the quotes from text file by "\n" (new line), then get its length and generate a random number between 0 to maximum length. Then use the generated number as index to get the quote on that particular line. Display it in your HTML element.

<button id="btn_modal">Give me a Quote</button>
<p id="kowts"></p>

#kowts {
    display:none;
    font-weight:bold
}

BTW, the text file that I used is not mine, got from somewhere else I forgot the link. (:

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Add/Subtract Single Digit Number in Assembly Language