// given an html response, extract the title
function getTitle(data) {
var matches = data.match(/<title>(.+)<\/title>/);
return matches.length > 1? matches[1] : '';
}
// find the body tag of an element
// because browsers parse the innerHtml differently
// (http://stackoverflow.com/questions/2488839/does-jquery-strip-some-html-elements-from-a-string-when-using-html)
// we can't rely on just $(data) to do this right
function getBody(data) {
var matches = data.match(/<body>(.+)<\/body>/);
return $(matches[1]);
}
//given an html respones, extract a description
function getDesc(data) {
var $data = $(data);
var $match = $data.find('meta[name=description]');
if ($match.length) {
return $match.attr('content');
}
var $body = getBody(data);
return $body.text().substring(0, 255).replace(/\n/, ' ');
}
// this url would point to a proxy (PHP) script on your server
// which would do a curl or similar operation to retrieve the
// url's contents; we just point to fiddle's simulator here
$.ajax('/echo/html/', {
data: {
// here you would pass the url you want to load to your
// proxy script so it can fetch it; we're just cheating here
// by using fiddle's response mechanism
html: "<html><head><title>The Title</title><meta name='description' content='This is a hello world script' /></head><body><div>Hello World!</div></body></html>",
delay: 1
},
success: function(data, status, xhr) {
//console.log(data);
//console.log(status);
//console.log(xhr);
$('#log').append('<h3>'+status+'</h3>')
.append('<p>title='+getTitle(data)+'</h4>')
.append('<p>desc='+getDesc(data)+'</p>');
},
type: 'POST',
error: function(xhr, status, err) {
$('#log').append('<h3>' + status + '</h3>').append('<p>' + err + '</p>');
},
dataType: 'text'
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.