Iframe button that adds a value to parent variable

Hello,

I am trying to write a script that allows a button in an iframe to change the value of a variable that is in the parent page. I then want to have an if statement in the parent that will change the url of the iframe ( or even change the url of the parent)

Here’s what I have for the parent:

<html>
<head>
<iframe src="http://zaazoo.thewebbusters.com/button.html">
</iframe>
</head>
<body>
<script type="text/javascript">
var a=0
function addToA () {
	a=(a+1)
}
if(a==1) {
	window.location="http://www.google.com"
}
}
</script>
</body>
</html>

here’s what I have for the iframe

<html>
<head>
</head>
<body>
<div style=" position: absolute; left:80; top: 35;">
<input type="button" onclick="parent.addToa()" value="Next">
</div>
</body>
<html>

I figured this script should change the parent to the url, but it doesn’t. Does anyone know what event I could use to change the iframe url? the parent url?

Thanks again for all your help,
Dave

I’m sure this would work if only you’d call the same function that you wrote.

Also use the error console and you won’t find yourself here so often.

Hey Thanks Logic Ali,

I do realize that i wrote parent.addToa() instead of parent.addToA(), but neither of them get me any results.

Dave

One more time: Use the error console.

I don’t know what/where the error code console is

What browser(s) are you using?

I use IE9, firefox5, and Chrome 13.0.782.112

LMGTFY

Logic Ali,

such a fine character you are

Well, I don’t have any of those browsers so I can’t say for certain. But I do have an older Firefox.

If the menu bar has “Tools” there should be an “Error Console” under that.

If so, open that, go to the page, then look in the console for Errors.

Thank you Mittineague.

As I am new to javascript, I do not find the error console to be of much use. Plus the errors it is giving me is that the Parent.addToA() is not a function as it refers to the iframe. I think I can understand why because that function is scripted on the parent not the child. Anyway sorry for rambling. Do you have any suggestions to why the script may not be working?

Unless the error console has changed, I think it’s very helpful (once you understand what it’s saying) eg. I get

Error: syntax error
Source File: file:///C:/My%20Documents/TEST/davery.html
Line: 14
Source Code:
}

and after the click

Error: parent.addToa is not a function
Source File: file:///C:/My%20Documents/TEST/davery-button.html
Line: 1

The first error message points to an extra curly brace, the second suggests a sytax error

After removing the extra curly brace and changing the onclick to

<input type="button" onclick="addToa()" value="Next">

the error console shows

Error: addToa is not defined
Source File: file:///C:/My%20Documents/TEST/davery-button.html
Line: 1

this suggests a problem with the function.
So I renamed addToa to addToA (they gotta be the same!)

To simplify debugging, I put everything in one file - BUT
I would take the iframe out of the <head> and put it into the <body> where <iframe>s belong. And I put the <script> in the <head>

This page gives no errors.

<html>
<head>
<script type="text/javascript">
var a=0
function addToA () {
	a=(a+1)
if(a==1) {
	window.location="http://www.google.com"
}
}
</script>
</head>
<body>
<div style=" position: absolute; left:80; top: 35;">
<input type="button" onclick="addToA()" value="Next">
</div>
</body>
</html>

Thank you so much for your help! It works perfectly