2017-09-13 24 views
-1

我需要創建一個返回iframe標記的JavaScript。 客戶會將腳本粘貼到iframe需要的位置,然後腳本應創建一個iframe。加載外部JavaScript文件並創建iframe

必須是這樣的:

<script src="http://www.helloworld.com/script/loadcustomerframe.js" data-customer="14532"></script> 

那麼腳本應該加載一個iframe來一個網址的地方,也是我需要閱讀「數據的客戶」。

我是後端開發人員c#,而不是前端。我已經嘗試了好幾天了,我不能讓它工作。

請幫忙。 感謝

回答

-1

像這樣的東西應該工作:

$(document).ready(() => { 
 
    $("[data-customer]").each(function() { 
 
    let customerId = $(this).data("customer"); 
 
    $(this).replaceWith(`<p>This is customer #${customerId}.</p>`); 
 
    // The following comment is an example of how you could use an iframe 
 
    //$(this).replaceWith(`<iframe src="http://example.org/customer/${customerId}>Hello!</iframe>`); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div data-customer="1"></div> 
 
<div data-customer="2"></div> 
 
<div data-customer="3"></div>

您只需要使用該腳本中出現一次(最有可能在你的<head>),它會取代任何<div>那具有data-customer屬性。您只需要找出的正確網址即可。

-2

硅基本上是你的JS代碼就可以了,

首先需要創建一個JS代碼,並將其託管到一些服務器通過獲取它得到它,所以像你的JS代碼託管到https://www.mygreatjscode.com/myjscode.js

所以,你的JS代碼會做休息,

這樣使用純JS(含未如jQuery等框架),讓您的myjscode.js文件將包含此:

//create an autoexec function 
(function(){ 
    var body = document.getElementByTagName("body"); 
    body = body ? body : false; 

    if (body){ 
     var iframe = document.createElement("iframe"); 
     iframe.setAttribute("id", "MY_CUSTOM_ID"); 
      //here set the url that the iframe will be render 
      iframe.setAttribute("src", "https://stackoverflow.com/"); 
      //finally insert into the body of page 
      body.appendChild(iframe); 
    } 

})(); 

最後,您需要將腳本標記插入到您的網頁中,就像這樣

<script src="https://www.mygreatjscode.com/myjscode.js" data-customer="14532"></script>