admin管理员组

文章数量:1290925

My iframe has a variable. I want to access document holding the iframe and play with a variable defined in javascript.

e.g.

<html>
<head>
<script>var a=0;</script> 

</head>
<body>

<iframe id=playmate src="document2.htm" height="150px" width="100%" scrolling="no"       
 border="0" frameborder="0"></iframe>>

</body>
</html>

Now in document2.htm I have a script that needs to access and manipulate var a. Can I access it directly, use jquery or plain vanilla javascript?

Its not a cross domain iframe. Promise. Same domain. Want them to play together nicely.

In the past I used to do this by getting the variable to insert a hidden input from one document into the other, but with the advent of ajax and what what I was wondering if I could just manipulate it.

So previously would write a function into the head of document, where if my var a did something, my hidden input in playmate document2 would update. Then document2 could just use hidden input and everyone was happy. When document2 played with the variable it would insert the result into hidden input in the holding document, and var a could then play properly with the new result.

I want to know if its now possible for playmate document2 to play with var a in the script without having to do hidden input. Would be very exciting.

Anyone got any ideas how to go about? I am using jquery if that would make it easier.

I tried using this script in document2:

 var c=window.document.parent.a;

But when I then output var c it tells me its notanumber. Bother. Please help. Or must I do my hidden input method.

My iframe has a variable. I want to access document holding the iframe and play with a variable defined in javascript.

e.g.

<html>
<head>
<script>var a=0;</script> 

</head>
<body>

<iframe id=playmate src="document2.htm" height="150px" width="100%" scrolling="no"       
 border="0" frameborder="0"></iframe>>

</body>
</html>

Now in document2.htm I have a script that needs to access and manipulate var a. Can I access it directly, use jquery or plain vanilla javascript?

Its not a cross domain iframe. Promise. Same domain. Want them to play together nicely.

In the past I used to do this by getting the variable to insert a hidden input from one document into the other, but with the advent of ajax and what what I was wondering if I could just manipulate it.

So previously would write a function into the head of document, where if my var a did something, my hidden input in playmate document2 would update. Then document2 could just use hidden input and everyone was happy. When document2 played with the variable it would insert the result into hidden input in the holding document, and var a could then play properly with the new result.

I want to know if its now possible for playmate document2 to play with var a in the script without having to do hidden input. Would be very exciting.

Anyone got any ideas how to go about? I am using jquery if that would make it easier.

I tried using this script in document2:

 var c=window.document.parent.a;

But when I then output var c it tells me its notanumber. Bother. Please help. Or must I do my hidden input method.

Share Improve this question edited Oct 25, 2011 at 18:56 John asked Oct 25, 2011 at 8:17 JohnJohn 3461 gold badge9 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You were close. It's actually:

window.parent.a

(window.parent returns a window object, and variables are children of the window object not the document object.)

本文标签: javascriptIs it possible to manipulate a variable in the parent window from within an iframeStack Overflow