-2

Possible Duplicate:
Getting contents of iframe

Javascript: I need two examples.

  1. Get content of loaded iframe which src is not on same server as parent page.
  2. Get content of loaded iframe which src is ON the same server as parent page.

By content I mean InnerTEXT or innerHTML, or anything. Goal is to transfer variable from one page that is not on same server to other. This was my first thought. If there is any other way to transfer variable within javascript.

Community
  • 1
  • 1
unknownz
  • 1
  • 1
  • postMessage sounds like what you're after: http://stackoverflow.com/questions/8394595/getting-value-from-iframe-to-iframe/8394621#8394621 – Jeffrey Sweeney Jan 17 '12 at 13:04

2 Answers2

1

Sounds like homework. What have you tried so far? Are you allowed to use jQuery? Show us your examples.

EDIT:

Since you are allowed to use jQuery try going down this path for your solutions:

 $('#iframe').contents().find('input').val();
Jay Tomten
  • 1,657
  • 1
  • 14
  • 23
  • I tried this: and then javascript var framecontent = document.getElementById("myframe").innerHTML; alert(framecontent) – unknownz Jan 17 '12 at 13:05
  • i also tried with window.frames['myframe'].document.body.innerHTML and many other ways, but it doesn't work, doesn't matter if frame src is on same server as parent or not. I am allowed to use jquery. – unknownz Jan 17 '12 at 13:06
0

It is not possible to transfer a variable from another website. But you can call functions which are defined in parent.

You make this function on your main site:

function helloWorld(text) {
  alert(text);
}

And you call from inside of the iFrame with parent.helloWorld('hiho!');. This should work with the same or any different domain, but you need to access the different page, to add the method call!

Armin
  • 15,582
  • 10
  • 47
  • 64