简体   繁体   English

在当前脚本标记位置插入iframe?

[英]Insert iframe at current script tag location?

I've got what we'll just call a "widget"...and I want to insert that widget (ultimately just an iframe ) at the current location of the script tag. 我已经得到了我们称之为“小部件”的东西......我想在脚本标签的当前位置插入该小部件(最终只是一个iframe )。

So you might have: 所以你可能有:

<p>Some example text</p>
<script src="http://example.com/widget.js" class="widget" async></script>
<p>More text</p>

In that widget.js file, there is this: 在那个widget.js文件中,有这样的:

var createIframe = function() {
  var parent = document.getElementsByClassName('widget');
  var iframe = document.createElement('iframe');

  iframe.src = '//example.com'

  // Works, but just inserts it at the end
  document.body.appendChild(iframe);

  // Does not work...just doesn't seem to do anything
  document.createElement("div").appendChild(iframe);
}

window.onload = function() {
  createIframe();
}

And as noted in the code comment, doing document.body.appendChild(iframe) works fine, but just inserts the iframe at the very end of the document. 并且如代码注释中所述,执行document.body.appendChild(iframe)工作正常,但只是在文档的最末端插入iframe。

But what I really want to do is just insert the iframe right where the script tag is. 但我真正想要做的只是在脚本标记所在的位置插入iframe。 That document.body.appendChild(iframe) is my attempt at it, but it literally just doesn't seem to do anything (no iframe and no errors). 那个document.body.appendChild(iframe)是我的尝试,但它实际上似乎没有做任何事情(没有iframe,没有错误)。

So, how can I insert the iframe in the same spot as the script tag? 那么,如何将iframe插入与脚本标记相同的位置?

You need to get the current <script> tag , which in this case is the last one: 您需要获取当前的<script>标记 ,在这种情况下是最后一个:

var thisScript = document.scripts[document.scripts.length - 1];

and insert the <iframe> after it: 并在其后面插入 <iframe>

var parent = thisScript.parentElement;
parent.insertBefore(iframe, thisScript.nextSibling);

http://jsfiddle.net/mattball/Tz75x/ http://jsfiddle.net/mattball/Tz75x/

Or simply replace the current script with the <iframe> : 或者只需 <iframe> 替换当前脚本:

var parent = thisScript.parentElement;
parent.replaceChild(iframe, thisScript);

http://jsfiddle.net/mattball/a23XE/ http://jsfiddle.net/mattball/a23XE/


Alternately – if you can believe it – document.write() is actually a suitable solution here , so long as you do it while the document is loading and not in a window.onload callback: 或者 - 如果你可以相信它 - document.write()实际上是一个合适的解决方案 ,只要你在文档加载时这样做不是在window.onload回调中:

function createIframe() {
    document.write('<iframe src="//example.com"></iframe>');
}

createIframe();

http://jsfiddle.net/mattball/2YCcP http://jsfiddle.net/mattball/2YCcP

Add an ID to your <script> element and query this element from the DOM, eg <script id="myscript" src="http://example.com/widget.js" class="widget" async></script> . <script>元素添加ID并从DOM中查询此元素,例如<script id="myscript" src="http://example.com/widget.js" class="widget" async></script>

In widget.js : widget.js

var scriptElement = document.getElementById("myscript");
var parentElement = scriptElement.parentElement;
parentElement.replaceChild(iframe, scriptElement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM