I have file http://host1.com/links.txt
links.txt contain:
com http://host1.com/1.jpg
info http://host1.com/2.jpg
org http://host1.com/3.jpg
I need to put in this code src link from that file depending on host domain.
a=document.getElementsByTagName('body')[0];
st='iframe';
r=st;
b=document.createElement(r);
b.src=**Here from links.txt**
b.width=300;b.height=300;b.marginHeight=10;b.marginWidth=10;b.frameborder=10;b.align='left';
a.appendChild(b);
for example I have 3 other sites
1. http://site1.com
2. http://site2.info
3. http://site3.org
In each site index.php I need put that iframe code, and in source code of:
http://site1.com/index.php I must have b.src=http://host1.com/1.jpg
http://site1.info/index.php I must have b.src=http://host1.com/2.jpg
http://site1.org/index.php I must have b.src=http://host1.com/3.jpg
How can I do that?
If you have control over the links.txt
and can change it to JSON, you can use JSONP to read it.
You can get the hostname by using window.location.hostname
.
links.php
<?php header('content-type: application/json; charset=utf-8'); ?>
(function(){
var data = {
com: 'http://host1.com/1.jpg',
info: 'http://host1.com/2.jpg',
org: 'http://host1.com/3.jpg'
};
<?php echo $_GET['callback']; ?>(data);
})();
javascript
$(function(){
$.ajax({
type: 'GET',
url: 'http://mysite.com/links.php?callback=?',
async: false,
jsonpCallback: 'jsonpCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
alert(json.info);
alert(json.com);
alert(json.org);
}
});
});