I would like to use a local variable of a parent in child window. I used parent.window.opener
but it returns undefined
.
This is my code:
<script type="text/javascript">
var selectedVal;
$(document).ready(function () {
//....
//...
if ($(this).val() == "byActor"){
$("#tags").focus();
$("#tags").autoplete({
source: "actorsauto.php",
minLength: 2,
focus: function( event, ui ){
event.preventDefault();
return false;
},
select: function (event, ui){
var selectedVal = ui.item.value;
alert(selectedVal);
}
});
});
$('#btnRight').on('click', function (e) {
popupCenter("movieByactor.php","_blank","400","400");
});
</script>
</body>
</html>
and this is a child:
<body>
<script type="text/javascript">
var selectedVal = parent.window.opener.selectedVal;
alert(selectedVal);
</script>
</body>
I would like to use a local variable of a parent in child window. I used parent.window.opener
but it returns undefined
.
This is my code:
<script type="text/javascript">
var selectedVal;
$(document).ready(function () {
//....
//...
if ($(this).val() == "byActor"){
$("#tags").focus();
$("#tags").autoplete({
source: "actorsauto.php",
minLength: 2,
focus: function( event, ui ){
event.preventDefault();
return false;
},
select: function (event, ui){
var selectedVal = ui.item.value;
alert(selectedVal);
}
});
});
$('#btnRight').on('click', function (e) {
popupCenter("movieByactor.php","_blank","400","400");
});
</script>
</body>
</html>
and this is a child:
<body>
<script type="text/javascript">
var selectedVal = parent.window.opener.selectedVal;
alert(selectedVal);
</script>
</body>
Share
Improve this question
asked Nov 11, 2014 at 11:57
mOnamOna
2,45910 gold badges37 silver badges63 bronze badges
1
-
Are you sure you want both
parent
andopener
? Also, are you attempting to readselectedValue
before it is set to a value? – Paul S. Commented Nov 11, 2014 at 11:58
2 Answers
Reset to default 12You can't - the whole idea with local variables is that they are only available in whatever function scope they are declared in - and functions inside that function.
In your case select selectedVal
is only available inside this function declaration:
select: function (event, ui){
var selectedVal = ui.item.value;
alert(selectedVal);
}
To use it outside this scope you need to make it global by attaching it to the window:
window.selectedVal = 'somevalue';
You can also make variables implicitly global by leaving out the var
keyword - however this is a poor practice and is not allowed in strict mode.
This will allow you to you access window.selectedVal
by:
window.opener.selectedVal // for windows opened with window.open()
window.parent.selectedVal // iframe parent document
try this:
<body>
<script type="text/javascript">
var selectedVal = window.opener.selectedVal;
alert(selectedVal);
</script>
</body>