cross-domain-requests-with-jquery
HTML
<!--
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
http://pastie.org
http://icant.co.uk/articles/crossdomain-ajax-with-jquery/error-handling.html
-->
<span id="msg"></span>
<div id="target">
</div>
JavaScript
$(function() {
var container = $('#target');
var msg = $('#msg');
var url = 'http://peterbenoit.com';
$.getJSON("http://query.yahooapis.com/v1/public/yql?" +
"q=select%20*%20from%20html%20where%20url%3D%22" +
encodeURIComponent(url) +
"%22&format=xml'&callback=?",
function(data) {
if (data.results[0]) {
var data = filterData(data.results[0]);
msg.html('(ready.)');
container.html(data).focus().effect("highlight", {}, 1000);
} else {
msg.html('(error!)');
msg.addClass('error');
var errormsg = '<p>Error: could not load the page.</p>';
container.html(errormsg).focus().effect('highlight', { color: '#c00' }, 1000);
}
}
);
});
function filterData(data) {
// filter all the nasties out
// no body tags
data = data.replace(/<?\/body[^>]*>/g, '');
// no linebreaks
data = data.replace(/[\r|\n]+/g, '');
// no comments
data = data.replace(/<--[\S\s]*?-->/g, '');
// no noscript blocks
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
// no script blocks
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
// no self closing scripts
data = data.replace(/<script.*\/>/, '');
// [... add as needed ...]
return data;
}