I am trying to open a fancybox iframe on my page. Pass over some basic information to the iframe. Then I want to make it so that the iframe talks back to it's parent.
I am passing nameid-1 throughout statically, though I would really like to have this as variable such as: var nameid=$(this).attr('nameid')
I just don't know how to execute this all correctly as I am new to Ajax/Javascript and struggling with the logic.
Base.html
JS:
<script type='text/javascript'>
//<![CDATA[
// Popup Function
$(document).ready(function () {
$('a.openinformation').fancybox({
openEffect: 'fade',
openSpeed: 500 //,
});
});
// Update from iFrame
function setInformation(userText) {
$('#displayfield-nameid-1').html(userText);
$('#showhide-nameid-1').show();
}
//]]>
</script>
HTML:
<div>
<a class="openinformation fancybox.iframe" href="iframe.html" nameid= "1" originalname="Mary Poppins" >Mary Poppins</a>
</div>
<div id ="showhide-nameid-1" style=" display:none; background:#0CF;">
<p>Replacement Name: <span id="displayfield-nameid-1"></span></p>
</div>
iframe.html
JS :
<script type='text/javascript'>
//<![CDATA[
// Start
$(window).load(function () {
// When Loaded get going.
$(document).ready(function () {
$('a.doupdate').click(function () {
parent.setInformation($(this).text());
parent.$.fancybox.close();
});
$('a.closeremove').click(function () {
parent.$('#showhide-nameid-1').hide();
parent.$.fancybox.close();
});
});
});
//]]>
</script>
HTML
<p>The old name: $originalname;</p>
<p>The id for this column is: $nameid</p>
<p>Please select a new name:</p>
<div><a class="doupdate" href="#">Display new married name : Mary Smith</a></div>
<div><a class="doupdate" href="#">Display new married name: Sandy Shore</a></div>
<div><a class="closeremove" href="#" id="1">Clear (Hide) married Names Box</a></div>
Your question can be dived in two parts :
1). Pass data from parent page to (fancybox) iframe
I think your best choice is to store all your data in a single javascript object like :
... so you can pass a single object to the iframe instead of several variables. Then you can add different properties and values to that object like :
... or more if you need so.
You still may want to pass that information statically through (HTML5)
data
attributes like :... and push the
data
values into theparentData
object within the fancyboxbeforeLoad
callback like :... that would give you much more flexibility IMHO.
Now, the only thing you need to do in the iframed page is to refer to those properties as
parent.parentData.nameid
andparent.parentData.originalname
any time you need them, e.g.having this html (iframe.html)
... you can use this script to write the values of the parent object like :
Notice you cannot do (as in php)
... so we used
<span>
tags to write their content via javascript.2). Pass data from iframed page to parent page.
First thing you need to do is to declare in your parent page, an object to store data from the iframe and a function to process it like :
Then in the iframed page, you can write different properties/values to the
iframeData
object and run thesetInformation()
function (in the parent page) from the iframe to pass the values to the parent page like :The code above assumes you have a similar html like
... notice I wrapped the name I want pass in a
span
tag. Optionally you could separate it in 2spans
like :... and write them in separated values like :
For the clear button, I would just reinitialize the variable and close fancybox like
... and perform the manipulation of the parent page from the parent page itself using the fancybox afterClose callback like :
... notice I will only show the selector
#showhide-nameid-1
if theiframeData
object's length is bigger than0
. Because that, I need a function to validate the object'slength
:Based on this answer, you could do:
... which will return the object's
length
.Last note :
Since the iframed page is referring to the parent page using the prefix
parent
, it will return js errors if it's opened outside an iframe. You may want to validate first if the iframed page is actually contained inside an iframe before trying to access data back and forth to/from the parent page like :See DEMO and feel free to explore the source code of both pages.