views:

156

answers:

2

I have an iframe on a page that allows us to upload an image, once it's uploaded it displays the url of the file in a text input field.

On the main part of the page, we have our textarea where we write up our news posts. I'd like a way so that I can add a link on the iframe, next to the url field, that when clicked will automatically insert the text in there into the textarea on the main part of the page.

I'm relatively new to javascript, so I wasn't sure how to do this when using an iframe.

Thanks!

A: 

Assuming I understand this correctly you want to be able to use javascript to access information in an iFrame from the container page. This is generally regarded as Cross Site Scripting and is not allowed.

Corey Sunwold
+1  A: 

Main.htm

<html>
<head>
<script>
getText = function(s)
{
alert(s);
}
</script>
</head>
<body>
<iframe src="otherFrame.htm"></iframe>
</body>
</html>

otherFrame.htm

<html>
<head>
<script>
botherParent = function()
{
    parent.getText(document.getElementById("textInput").value);
};
window.onload = function()
{
}
</script>
</head>
<body>
<input type="text" id="textInput" />
<span onclick="botherParent()">Stuff goes here</span>
</body>
</html>

Basically, in the child inline frame, use parent to access the parent's window object. Global variables are stored in window, as are global functions. Because of this, if you define a global variable "foo" in parent, you can access it with parent.foo with your child frame.

Hope that helps!

ItzWarty
Thanks, helped me figure out the right direction to head toward.
scatteredbomb