2017-09-13 62 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>