Jonathan
Jonathan

Reputation: 641

How to make links work in Twitter Tweets? API + json

Can anybody make my twitter links work?
I tried to find the answer on the Twitter API page, but can't find it..

See here my jsfiddle.

Thank you in advanced!
With Kind Regards,
Jonathan

Upvotes: 1

Views: 1559

Answers (2)

tuoxie007
tuoxie007

Reputation: 1236

var words = _this.text.split(/\s+/);
for (var i=0; i<words.length; i++) {
    var word = words[i];
    if (word.substr(0, 7) == 'http://' || word.substr(0, 8) == 'https://') {
        words[i] = '<a href="'+word+'">'+word+'</a> ';
    }
}
var text = words.join(' ');

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Add this function to your code to parse URLs:

function parseLinks(tweet) {
    return tweet.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function (tweet) {
        return tweet.link(tweet);
    });
};

And then parse the text of the tweet through this function before displaying it:

timeline.append(
    $('<li>').append(
        $('<strong>').text(created_at),
        $('<span>').html('<br />' + parseLinks(_this.text))
    )
);

Example fiddle

Upvotes: 2

Related Questions