douguanya4248 2013-01-10 18:48
浏览 25
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douzhengyi5022 2013-01-10 18:53
    关注

    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);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法