admin管理员组

文章数量:1419640

I have a javascript code which is called inside an iframe. I want to add a tag inside that javascript code and append the tag to the iframes parent body. This is my code to add the script tag and append to iframes parent body.

setTimeout(function () {
         var scriptTag = "<script>alert(1)<";
         scriptTag +=  "/script>";
         $("#iframe").contents().find("body").append(scriptTag);
    }, 5000);

the code isn't working it neither added the script tag nor showed any error

I have a javascript code which is called inside an iframe. I want to add a tag inside that javascript code and append the tag to the iframes parent body. This is my code to add the script tag and append to iframes parent body.

setTimeout(function () {
         var scriptTag = "<script>alert(1)<";
         scriptTag +=  "/script>";
         $("#iframe").contents().find("body").append(scriptTag);
    }, 5000);

the code isn't working it neither added the script tag nor showed any error

Share Improve this question edited Jan 13, 2017 at 7:38 Tina asked Jan 13, 2017 at 7:17 TinaTina 711 silver badge7 bronze badges 8
  • 1 and what is the question? – Arkej Commented Jan 13, 2017 at 7:18
  • the code isn't working!!!!!!!!! it neither added the script tag nor showed any error – Tina Commented Jan 13, 2017 at 7:20
  • what is the exact requirement? – M14 Commented Jan 13, 2017 at 7:23
  • i have 2 js files, the first js file is called inside an iframe i want to call the 2nd file in the first js file and append it inside the parent body of the iframe – Tina Commented Jan 13, 2017 at 7:25
  • Import that other JS file using <script src=".."/> and then you can continue to use that(i.e. functionalitis provided by 2nd file). Why do you want to copy the content the of the 2nd file to 1st file? – Anurag Sinha Commented Jan 13, 2017 at 7:29
 |  Show 3 more ments

5 Answers 5

Reset to default 3

Thanks guys for all the ans I did figured out a way to add the tag and append it to the iframe's parent body.

setTimeout(function () {                     
              var imported = document.createElement('script');
              imported.src = 'src';
             parent.document.body.append(imported);
       }, 5000);

It may work better if you create a new script element and add that to the parent.

See this post for more information: document.createElement('script') vs <script src="">

This is an example copied from the accepted answer in the above link.

var s = document.createElement('script');
s.src = "...";
document.getElementsByTagName("head")[0].appendChild(s);

The src property is the URL containing the script.

To add the script content to the element directly from a string you need the answer from: Adding <script> element to the DOM and have the javascript run?

So your question example code would be:

var scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
var code = 'alert(1);';
try {
  scriptTag.appendChild(document.createTextNode(code));
  $("#iframe").contents().find("body").appendChild(scriptTag);
} catch (e) {
  scriptTag.text = code;
  $("#iframe").contents().find("body").appendChild(scriptTag);
}

Also see this post for more details on how to attach the script element: document.head, document.body to attach scripts

Your scriptTag is a string and not an HTML element, so you are just appending the string. You have to use document.createElement

setTimeout(function () {
var scriptTag = document.createElement('script');
     scriptTag.src = 'alert(1)'
     $("#iframe").contents().find("body").append(scriptTag);
}, 5000);

You can use this method using vanilla javascript.
this work for me I guaranteed this work for you to

function insertScript(script) {
  var head = document.head;

  var createScript = document.createElement('script');
  createScript.innerHTML = script;
  createScript.setAttribute('defer', '');

  head.appendChild(createScript);
}

let myScript = `
  var a = 1 + 2;
  alert(a);
`; // You can write anything you want

insertScript(myScript);

This might help

setTimeout(function () {
     var scriptTag = "<script>alert(1)</" + "script>";
     $("#iframe").contents().find("body").append(scriptTag);
}, 5000);

本文标签: jqueryHow to write a script tag inside a javascript codeStack Overflow