簡體   English   中英

來自域名的文件中的Javascript src

[英]Javascript src from file depending on domain name

我有文件http://host1.com/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

在每個網站index.php中,我需要放置iframe代碼,並在以下代碼中添加:

http://site1.com/index.php我必須擁有b.src = http://host1.com/1.jpg

http://site1.info/index.php我必須擁有b.src = http://host1.com/2.jpg

http://site1.org/index.php我必須擁有b.src = http://host1.com/3.jpg

我怎樣才能做到這一點?

如果您可以控制links.txt並將其更改為JSON,則可以使用JSONP讀取它。

您可以使用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);
        }
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM