A nifty cross domain URL fetcher

The same origin policy in browsers can sometimes be a hindrance and headache. All you want to do is fetch some html from some url, scrape it, do magical stuff with it for your all-too-shiny new mash-up, but this damn cross-domain restrictions! Well, that's where JSONP comes in.

Here is a github repository for a simple JSONP url source provider : http://fetch-url.appspot.com

Have a look at sample code to get a flavor of it.
function myCallback (json) {
    var page_source = json.source;
    // do stuff with page source here.
    
    // for example, counting links.
    alert ("The page has " + $(page_source).find('a').length + " links.");
}

var url = "http://google.com";

$.ajax({
  url: "http://fetch-url.appspot.com"
               + "?url=" + url
               + "&funcname=myCallback",
  dataType: "jsonp"
});
Enter url of a page:

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.