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.










share|improve this question
















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 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















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.










share|improve this question
















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 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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 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
















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










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
}





share|improve this answer





















  • 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










  • 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


















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.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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
    }





    share|improve this answer





















    • 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










    • 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















    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
    }





    share|improve this answer





















    • 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










    • 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













    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
    }





    share|improve this answer












    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
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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










    • 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










    • 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










    • 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












    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 26 at 16:59









        user177933

        111




        111






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'