Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iframe referencing parent 1

Status
Not open for further replies.

Hobeau

MIS
Apr 19, 2004
77
US
Hey guys, I have a webpage that has an Iframe in it. I'm trying to write a javascript that is going to reference the parent page. Is there a way to do this?
 
In the iframe? You'd use the parent object. I can't really help you more since no specifics were given.

Here's a vague example:

Code:
<a href="#" onclick="parent.document.formname.elementname.value = 'hi';">Populate parent field</a>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Thanx for the quick response,, this is my code so far, I'm using javascript (I'm still a newbie at it though). This is my function.

Code:
<script language="javascript">
function changetext(text)
if text=="a" 
parent.document.test.value="testing testing 123";
else if text=="b"
parent.document.test.value="dude,,, check it out";
</script>

I'm using an image to call the function using an "onclick"

Code:
<img src="TF23_03_WT.jpg" width=85 height=85 onclick="changetext("a");">
 
I noticed a few things. Here's the general concept:

1) functions need to be enclosed with curly braces { }
2) conditional statements (if something) need to have parentheses
3) watch out for overlapping quotation marks
Valid: "changetext('a');"
Invalid: "changetext("a");"

So, I suggest the following updates to your code:

Code:
<script language="javascript">
[red]<!--[/red]
function changetext(text) [red]{[/red]
    if [red]([/red]text == "a"[red])[/red]
        parent.document.test.value="testing testing 123";
    else if [red]([/red]text == "b"[red])[/red]
        parent.document.test.value="dude,,, check it out";
[red]}[/red]
[red]-->[/red]
</script>

Code:
<img src="TF23_03_WT.jpg" width=85 height=85 onclick="changetext('a');">

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Yeah thanx,, I'm still new so I'm making dumb mistakes. I modified the code and its still being mean to me and not working. The element I'm referencing is a <div></div> element that I have ID'ed as "test" for now. The <div> element is in the main HTML page, and the javascript function that is trying to reference it is in an Iframe. I found some code that had the same type of thing that had an property called "innerHTML" (parent.document.elementname.innerHtml="text text"). I tried that and that didn't work either. Should I somehow include the iframe id in this?

Thanx
 
What is it exactly that you're trying to do? Put a value in a field? Or put text inside a DIV? Where does the item you want to show data in reside? Where is the source?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
I'm trying to put text inside a DIV. The function is in the Iframe (including the text that I want to use).
 
Ahh! ok. Then try:

Code:
parent.document.getElementById('test').innerHTML = "dude,,, check it out";

If this doesn't work, there's something wrong elsewhere in the code...

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
oh yeah,,, awesome,,, thank you so much for your help,,, I'd give you 5 stars if I could lol.
 
All set now?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top