Php 来自文件的Javascript src取决于域名
我有文件 links.txt包含:Php 来自文件的Javascript src取决于域名,php,javascript,iframe,Php,Javascript,Iframe,我有文件 links.txt包含: com http://host1.com/1.jpg info http://host1.com/2.jpg org http://host1.com/3.jpg 根据主机域的不同,我需要从该文件中输入这个代码src链接 a=document.getElementsByTagName('body')[0]; st='iframe'; r=st; b=document.createElement(r); b.src=**Here from links.txt*
com http://host1.com/1.jpg
info http://host1.com/2.jpg
org http://host1.com/3.jpg
根据主机域的不同,我需要从该文件中输入这个代码src链接
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);
例如,我有3个其他网站
1. http://site1.com
2. http://site2.info
3. http://site3.org
在每个site index.php中,我需要输入iframe代码,并在源代码中输入:
我一定要有b.src=http://host1.com/1.jpg
我一定要有b.src=http://host1.com/2.jpg
我一定要有b.src=http://host1.com/3.jpg
我该怎么做呢?如果您可以控制links.txt并可以将其更改为JSON,那么您可以使用来阅读它 您可以使用window.location.hostname获取主机名 links.php
我只能使用javascript或PHPJSONP/jQuery is javascriptI我已经更新了我的示例,以便更好地了解它的工作原理。
<?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);
})();
$(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);
}
});
});