0

The code below will add an iframe dynamically, is there a way to insert javascript to this iframe dynamically?

Thx

  var iFrame = document.createElement('iframe');
  var iFrameSRC = "http://www.google.com";
  iFrame.name = 'hehe';
  iFrame.id = 'hehe';
  iFrame.src = iFrameSRC;
  iFrame.width = '1px';
  iFrame.height = '1px';
  iFrame.frameBorder = 0;
  iFrame.style.overflow = "hidden";
  iFrame.style.align = "left";
  iFrame.style.display = "block";
  iFrame.style.fontSize = "10px";

  bodyInner.appendChild(iFrame);

3 Answers 3

2

I think it will be something like this:

var script=iframe.createElement('script');

    script=iframe.standardCreateElement('script');
    script.setAttribute('src','http://localhost/jquery-1.3.2.js');
    script.setAttribute('type','text/javascript');
    iframe.lastChild.firstChild.appendChild(script);

Note this can be done just if the iframe and the holder page are in the same domain, or if you calling this from a bookmarklet or a browser extension/plugin.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work... Maybe you are trying to create something like this:

<body onload="initialize()">
<script language="JavaScript">
function initialize() {
  var testFrame =
        document.createElement("IFRAME");
  testFrame.id = "testFrame";
  testFrame.name = 'hehe';
  testFrame.src = "http://www.seekload.tk";
  testFrame.style.border='20px';
  testFrame.style.width='900px';
  testFrame.style.height='300px';
  testFrame.style.frameBorder = 10;
  testFrame.style.overflow = "hidden";
  testFrame.style.align = "left";
  testFrame.style.display = "block";
  testFrame.style.fontSize = "10px";
  document.body.appendChild(testFrame);
}
</script>

PS: You made some mistakes in your coding try the above instead.

Comments

-1

No, you cannot insert code into a document from another domain. This is a security measure enforced by the browser to prevent cross-site scripting (XSS) and phishing attacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.