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 badges1 Answer
Reset to default 10You 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.)
版权声明:本文标题:javascript - Is it possible to manipulate a variable in the parent window from within an iframe? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515673a2382873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论