1

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

Tina
  • 71
  • 1
  • 7
  • 1
    and what is the question? – Arkej Jan 13 '17 at 07:18
  • the code isn't working!!!!!!!!! it neither added the script tag nor showed any error – Tina Jan 13 '17 at 07:20
  • what is the exact requirement? – M14 Jan 13 '17 at 07: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 Jan 13 '17 at 07:25
  • Import that other JS file using 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 Jan 13 '17 at 07:29
  • i had tried that the js file got loaded but the js file is not getting append to iframe's parent body – Tina Jan 13 '17 at 07:35
  • If both the parent and the child page have the same origin (≅ domain name), you can do `window.parent.document.body.insertAdjacentHTML('beforeend', scriptTag);` – Boldewyn Jan 13 '17 at 07:45
  • thanks for the help but got this text "[object HTMLScriptElement]" instead of the tag – Tina Jan 13 '17 at 08:20

5 Answers5

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);
Tina
  • 71
  • 1
  • 7
1

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

Community
  • 1
  • 1
Glenn
  • 46
  • 4
0

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);
Ropo Hage
  • 26
  • 2
0

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);
Rian
  • 1
  • 1
-1

This might help

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