javascript - Detecting & displaying links in a form's text area - Stack Overflow

When I enter a link (video, image, URL, etc.) in Facebook's "What's on your mind?"

When I enter a link (video, image, URL, etc.) in Facebook's "What's on your mind?" form, it auto-detects the link and converts it to a thumbnail with a brief description below the text-area. Can anyone provide me with insight or a link to get me going on how to achieve this?

When I enter a link (video, image, URL, etc.) in Facebook's "What's on your mind?" form, it auto-detects the link and converts it to a thumbnail with a brief description below the text-area. Can anyone provide me with insight or a link to get me going on how to achieve this?

Share Improve this question asked Sep 6, 2012 at 14:46 hugohugo 1,2651 gold badge14 silver badges28 bronze badges 1
  • Make an extensive program that detects the domain of the link, and then retrieves meaningful information from said link, and then displays it in a consistent presentable fashion. In other words, you are on your own on this one, as that is no simple piece of code. – thatidiotguy Commented Sep 6, 2012 at 14:49
Add a ment  | 

3 Answers 3

Reset to default 2

There's a javascript attached to the textarea change event. The javascript detects if the content of the textarea is a url, if it is, the javascript call a webservice that visit the url looking for the page title, the page description, etc, (or the open graph protocol meta tags), if it find each one of the tags they are returned to the javascript who proper organize then.

Facebook also cache this content, and if the same url is posted by another user, he uses the cache values instead of revisiting the page.

The open graph protocol meta tags:

http://developers.facebook./docs/opengraphprotocol/

using something like

var input = document.getElementById("textarea");
input.addEventListener("change", checkLink(e));
input.addEventListener("blur", checkLink(e));

function checkText(text){
     var exp = "((ht|f)tp(s?))(:((\/\/)(?!\/)))(((w){3}\.)?)([a-zA-Z0-9\-_]+(\.(|edu|gov|int|mil|net|org|biz|info|name|pro|museum|co\.uk)))(\/(?!\/))(([a-zA-Z0-9\-_\/]*)?)([a-zA-Z0-9])+\.((jpg|jpeg|gif|png)(?!(\w|\W)))";
     return text.match(exp);
}
function checkLink(e){
     //here you would want to use a regular expression and check for http:

     var regularExpression = !!checkText(e.target.innerHTML); // returns true or false
     if(regularExpression){
       e.target.innerHTML += "<a href='#'><img src="" alt="" /></a>";
     }
}

good resource for regular expressions - http://regexlib./Search.aspx?k=image&c=-1&m=-1&ps=20

Warning -- have to leave for work so regular expressions are not checked.

Take the link value and run it through a regular expression that looks for ^http:...[^\s] or ^https:...[^\s] and returns those.

Then, pass those URLs to your server and have your server retrieve the document and return a snippit for you to then put in your document. You must have your own server to help because Javascript, by itself, has security restrictions. Google same origin policy for more info.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745372507a4624856.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信