Code for jQuery read from other domains
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!
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:
- change ‘
yourdomain‘, in the script you see in the header, in -duh- your domain name - 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.
December 13th, 2008 at 1:50 am
[...] with all its explanations. Have a look at the cool demo first. Than copy the complete, small html and php scripts. Thunderbirds are [...]
February 1st, 2011 at 2:39 pm
Hello!
I think here must be missing “load(” -
$(“#allh2″).load(“http://www.yourdomain.com/loader.php
Your code is:
$(“#allh2″).”http://www.yourdomain.com/loader.php
February 4th, 2011 at 2:20 am
Thank you so much! I edited the code.
April 9th, 2011 at 1:24 pm
The scripit did not work for me:
$(document).ready(function(){
$(“#allh2″).load(“http://weboldal.biz”,{want2Read:’http://www.yahoo.com/’},function(){
//Write your additional jQuery script below. For instance:
$(“h2″).attr(“class”,”coolheader”);
});
});
Wow, this stuff is need
All headers from want2read, I learned this from Sitegrind
April 9th, 2011 at 1:27 pm
I have changed to mydomaian, a changed the read domain to yahoo.com, and yahoo site did not appear on my site. I thought that should have been the intent.
Thanks
August 21st, 2011 at 2:51 pm
This is the worst tutorial ever, dude! You copied and pasted a bunch of stuff and it doesn’t even work! lmao
August 23rd, 2011 at 9:01 pm
I’m sorry you are not able to make it work so far. Maybe the example page will make things clear.