Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 来自文件的Javascript src取决于域名_Php_Javascript_Iframe - Fatal编程技术网

Php 来自文件的Javascript src取决于域名

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*

我有文件

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);
        }
    });
});