将脚本插入已加载的iframe

时间:2014-12-03 11:24:44

标签: javascript jquery

我正在尝试将脚本插入到已加载的iframe中。没有任何运气试了这个;

document.write('<iframe src ="http://example.com/" width ="100" height ="100">
</iframe>');


document.getElementsByTagName('iframe')[0].onload = function() {

var curInnerHTML = document.body.innerHTML;
curInnerHTML = curInnerHTML.replace("</a>", "</a><scr" + "ipt>
document.write='Hi.......';</sc" + "ript>");
document.body.innerHTML = curInnerHTML;

  }

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

<script>
document.write('<iframe src ="http://example.com/" width ="100" height ="100"></iframe>');


        document.getElementsByTagName('iframe')[0].onload = function() {

        var curInnerHTML = document.body.innerHTML;
        curInnerHTML = curInnerHTML.replace("</a>", "</a>"+'<script>'+"document.write=Hi......."+"</sc"+"ript>");
        document.body.innerHTML = curInnerHTML;

          }

</script>