Setting a value of an iframe in the parent
up vote
1
down vote
favorite
This code works on Chrome 58 but it makes me feel dirty:
parent.parent.parent.document.F1.document.getElementById("txtNewDrugAction").value = "foobar"
F1
is the name of the iFrame
where the text box resides.
I am setting the value of an input box from a modal popup window (via jQuery UI). I've tried to use selectors etc, but to no avail. I feel like there would be a hugely easier way to do this that would also be more resilient to changes in the DOM but I can't see it just yet...
I also wind up writing code like this (again, functional, but awful):
window.top.document.getElementById("F1").contentWindow.document.getElementById("txtRefresh") = "foobar"
I'm maintaining a legacy application. Please nobody think that something that uses frames is being created in 2017, especially not by me.
javascript jquery
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
This code works on Chrome 58 but it makes me feel dirty:
parent.parent.parent.document.F1.document.getElementById("txtNewDrugAction").value = "foobar"
F1
is the name of the iFrame
where the text box resides.
I am setting the value of an input box from a modal popup window (via jQuery UI). I've tried to use selectors etc, but to no avail. I feel like there would be a hugely easier way to do this that would also be more resilient to changes in the DOM but I can't see it just yet...
I also wind up writing code like this (again, functional, but awful):
window.top.document.getElementById("F1").contentWindow.document.getElementById("txtRefresh") = "foobar"
I'm maintaining a legacy application. Please nobody think that something that uses frames is being created in 2017, especially not by me.
javascript jquery
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
you forgot the.value
in the last code snippet, so that will not function as is. also in that last snippet you can omitwindow
unless there is another variable in scope calledtop
.window
is global and therefore always in scope and therefore never needs to be stated.
– Occam's Razor
Jul 30 '17 at 5:47
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This code works on Chrome 58 but it makes me feel dirty:
parent.parent.parent.document.F1.document.getElementById("txtNewDrugAction").value = "foobar"
F1
is the name of the iFrame
where the text box resides.
I am setting the value of an input box from a modal popup window (via jQuery UI). I've tried to use selectors etc, but to no avail. I feel like there would be a hugely easier way to do this that would also be more resilient to changes in the DOM but I can't see it just yet...
I also wind up writing code like this (again, functional, but awful):
window.top.document.getElementById("F1").contentWindow.document.getElementById("txtRefresh") = "foobar"
I'm maintaining a legacy application. Please nobody think that something that uses frames is being created in 2017, especially not by me.
javascript jquery
This code works on Chrome 58 but it makes me feel dirty:
parent.parent.parent.document.F1.document.getElementById("txtNewDrugAction").value = "foobar"
F1
is the name of the iFrame
where the text box resides.
I am setting the value of an input box from a modal popup window (via jQuery UI). I've tried to use selectors etc, but to no avail. I feel like there would be a hugely easier way to do this that would also be more resilient to changes in the DOM but I can't see it just yet...
I also wind up writing code like this (again, functional, but awful):
window.top.document.getElementById("F1").contentWindow.document.getElementById("txtRefresh") = "foobar"
I'm maintaining a legacy application. Please nobody think that something that uses frames is being created in 2017, especially not by me.
javascript jquery
javascript jquery
edited Jun 26 at 4:34
Jamal♦
30.2k11115226
30.2k11115226
asked May 30 '17 at 23:06
Lewis Cianci
317
317
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
you forgot the.value
in the last code snippet, so that will not function as is. also in that last snippet you can omitwindow
unless there is another variable in scope calledtop
.window
is global and therefore always in scope and therefore never needs to be stated.
– Occam's Razor
Jul 30 '17 at 5:47
add a comment |
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
you forgot the.value
in the last code snippet, so that will not function as is. also in that last snippet you can omitwindow
unless there is another variable in scope calledtop
.window
is global and therefore always in scope and therefore never needs to be stated.
– Occam's Razor
Jul 30 '17 at 5:47
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
you forgot the
.value
in the last code snippet, so that will not function as is. also in that last snippet you can omit window
unless there is another variable in scope called top
. window
is global and therefore always in scope and therefore never needs to be stated.– Occam's Razor
Jul 30 '17 at 5:47
you forgot the
.value
in the last code snippet, so that will not function as is. also in that last snippet you can omit window
unless there is another variable in scope called top
. window
is global and therefore always in scope and therefore never needs to be stated.– Occam's Razor
Jul 30 '17 at 5:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
The simplest would be to use window.top
if F1
is part of the top-most window, or possibly go top down window.top.someframe.someotherframe.F1
.
An alternative might be to write a helper something like:
function getFrame(name) {
var current = this;
while (current) {
if (current.document[name])
return current.document[name].document;
current = current.parent;
}
return undefined; // not found
}
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS inF1
create a method, saywindow.setDrugAction(...)
then your code would begetFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.
– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returningundefined
. justreturn;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.
– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
add a comment |
up vote
0
down vote
Maybe I don't get your problem so well but couldn't you interact with the parent via html5 postMessage call? Definite the protocol and then you're more decoupled from the DOM too.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The simplest would be to use window.top
if F1
is part of the top-most window, or possibly go top down window.top.someframe.someotherframe.F1
.
An alternative might be to write a helper something like:
function getFrame(name) {
var current = this;
while (current) {
if (current.document[name])
return current.document[name].document;
current = current.parent;
}
return undefined; // not found
}
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS inF1
create a method, saywindow.setDrugAction(...)
then your code would begetFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.
– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returningundefined
. justreturn;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.
– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
add a comment |
up vote
0
down vote
The simplest would be to use window.top
if F1
is part of the top-most window, or possibly go top down window.top.someframe.someotherframe.F1
.
An alternative might be to write a helper something like:
function getFrame(name) {
var current = this;
while (current) {
if (current.document[name])
return current.document[name].document;
current = current.parent;
}
return undefined; // not found
}
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS inF1
create a method, saywindow.setDrugAction(...)
then your code would begetFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.
– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returningundefined
. justreturn;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.
– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
add a comment |
up vote
0
down vote
up vote
0
down vote
The simplest would be to use window.top
if F1
is part of the top-most window, or possibly go top down window.top.someframe.someotherframe.F1
.
An alternative might be to write a helper something like:
function getFrame(name) {
var current = this;
while (current) {
if (current.document[name])
return current.document[name].document;
current = current.parent;
}
return undefined; // not found
}
The simplest would be to use window.top
if F1
is part of the top-most window, or possibly go top down window.top.someframe.someotherframe.F1
.
An alternative might be to write a helper something like:
function getFrame(name) {
var current = this;
while (current) {
if (current.document[name])
return current.document[name].document;
current = current.parent;
}
return undefined; // not found
}
answered May 31 '17 at 1:26
Marc Rohloff
2,93236
2,93236
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS inF1
create a method, saywindow.setDrugAction(...)
then your code would begetFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.
– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returningundefined
. justreturn;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.
– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
add a comment |
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS inF1
create a method, saywindow.setDrugAction(...)
then your code would begetFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.
– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returningundefined
. justreturn;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.
– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
Yeah the helper is a good idea. How could I expand that to search through controls in that iframe as well? I couldn't think of how, with document.all being deprecated.
– Lewis Cianci
May 31 '17 at 4:04
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS in
F1
create a method, say window.setDrugAction(...)
then your code would be getFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.– Marc Rohloff
May 31 '17 at 15:42
You could use a second helper. iirc (as in, it's been a while since I messed around with this stuff) you could have your JS in
F1
create a method, say window.setDrugAction(...)
then your code would be getFrame('F1').setDrugAction('foobar')
. This would separate your responsibilities more cleanly.– Marc Rohloff
May 31 '17 at 15:42
there's no benefit to explicitly returning
undefined
. just return;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.– Occam's Razor
Jul 30 '17 at 5:20
there's no benefit to explicitly returning
undefined
. just return;
by itself will return undefined, or you can omit the return entirely and the function will return undefined.– Occam's Razor
Jul 30 '17 at 5:20
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
As far as the JS runtime is concerned you are right they are the same thing but if I am returning a specific value as a flag value I always put it in so that it is obvious to anyone reading the code what is happening. As a general rule I always return explicit values when defining a function that returns something.
– Marc Rohloff
Jul 30 '17 at 18:32
add a comment |
up vote
0
down vote
Maybe I don't get your problem so well but couldn't you interact with the parent via html5 postMessage call? Definite the protocol and then you're more decoupled from the DOM too.
add a comment |
up vote
0
down vote
Maybe I don't get your problem so well but couldn't you interact with the parent via html5 postMessage call? Definite the protocol and then you're more decoupled from the DOM too.
add a comment |
up vote
0
down vote
up vote
0
down vote
Maybe I don't get your problem so well but couldn't you interact with the parent via html5 postMessage call? Definite the protocol and then you're more decoupled from the DOM too.
Maybe I don't get your problem so well but couldn't you interact with the parent via html5 postMessage call? Definite the protocol and then you're more decoupled from the DOM too.
answered May 26 at 16:59
user177933
111
111
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f164564%2fsetting-a-value-of-an-iframe-in-the-parent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'll agree that it "feels" dirty, and it surely is sensitive to changes in the DOM.
– Gypsy Spellweaver
May 31 '17 at 1:03
google maps, basically all the google plugins, facebook plugins etc.. lots of things are being developed in 2017 using iframes...
– Occam's Razor
Jul 30 '17 at 5:13
you forgot the
.value
in the last code snippet, so that will not function as is. also in that last snippet you can omitwindow
unless there is another variable in scope calledtop
.window
is global and therefore always in scope and therefore never needs to be stated.– Occam's Razor
Jul 30 '17 at 5:47