Quantcast
Channel: Sitegrind » jQuery
Viewing all articles
Browse latest Browse all 2

Code for jQuery read from other domains

$
0
0

The code is in this quick tutorial about how to read content from websites on other domains and publish it on your own html site. All done using jQuery and a small php script you can copy. It belongs to the original article, where you can find more info. Don’t need that, just look at the demo and use these files!

UPDATE: 2012-05-23

This article was written in 2008. Please ee the original article for pointers about how to read html code from other domains using jQuery.

HTML file to read websites

Take this html.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#allh2").load(
"http://www.yourdomain.com/loader.php h2",{want2Read:'http://www.want2read.com/'},function(){
//Write your additional jQuery script below. For instance:
$("h2").attr("class","coolheader");
});
});
</script> </head> <body>
<h1>Wow, this stuff is need</h1>
<p class="optionalcontent">All headers from <a href="http://www.want2read.com/">want2read</a>, I learned this from <a href="http://www.sitegrind.nl/">Sitegrind</a></p>
<div id="allh2"> </div>
</body> </html>

In this html file, you must change 2 things:

  1. change ‘yourdomain‘, in the script you see in the header, in -duh- your domain name
  2. change ‘http://www.want2read.com/'in the domain you want to loan content from

This is what you leave alone:

  • want2Read, the first one in (want2Read:'http://www.want2Read.com/')
    • fyi: this is the name of a variable php is going to use
  • The $("#allh2") and <div id="allh2"> </div> ,
    • if you change, keep them complying with each other

What you can change. Save this for later, make files working first. Nevertheless, be inspired!:

  • Feel free to write you own test in the html file. Don’t forget to be gentle. Always put a reference to site you borrowed the content from.
  • the h2 after yourdomain ("http://www.yourdomain.com/loader.php h2" )
    • examples: h1, div#goodstuff, a[href*='domainname'], img.realones

This is where the good stuff happens! Although not clearly recognizable because of omitting $ sign, this is where you can use our so beloved jQuery selectors! I haven’t tested in how far the chain ability of jQuery applies to this special use of selector and its unusual semantics. Let me know if you tested. I like to use this as a first selection. And than use the callback function to perform further action.

save this file as index.html on your server.

The php to help jQuery load the website

Copy and paste this php in a text file and save on your server as loader.php, in the same directory as the index.html. Change nothing.

<?php
extract($_POST);
$str = file_get_contents($want2Read);
echo $str;
?>

Many of sites protect their content, if this doesn’t work you can use the alternate php code. See the original article.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images