how to passing value from popup to the parent window as a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perhapscwk
    New Member
    • Sep 2007
    • 123

    how to passing value from popup to the parent window as a variable

    how to passing value from popup to the parent window as a variable
    which I can access and process the variable(string ) in parent window?

    such as from popup page, we have var_from_popup= "abc", after the popup
    closed, then from parent window, we can use alert(var_from_ popup) and
    it will show us the "abc"?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use window.opener to refer to the parent, then you can set whatever variable you wish in the parent window.

    Comment

    • perhapscwk
      New Member
      • Sep 2007
      • 123

      #3
      there is a variable var varwindow in primary window;

      from popup, i set
      window.opener.v arwindow="abc"

      then from primary window , i use alert(varwindow ) but
      it write as "underfinde d" but not "abc"..why?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        try
        Code:
        alert(window.varwindow);
        regards

        Comment

        • perhapscwk
          New Member
          • Sep 2007
          • 123

          #5
          not okay..why? it still show underfine..


          from Mainpage.htm
          Code:
           
          var item1;
          window.open("popup.htm", "name", "width=255,height=455");
          alert(item1);
          from popup.htm
          Code:
           
          <script type="text/javascript">
          
          function get_value()
          {
             window.opener.item1="123";
          }
          </script>
          <form>
          <textarea name='itembox' rows=10 cols=30></textarea>
          <input type="button" onclick="get_value();window.close()" value="Submit">
          </form>

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by perhapscwk
            not okay..why? it still show underfine..
            you are mistaking properties with variables. try the code I posted.

            Comment

            • perhapscwk
              New Member
              • Sep 2007
              • 123

              #7
              I try alert(window.it em1) but still not okay..

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                It's going to be undefined because there simply isn't enough time before you alert it. When you call window.open, it's more or less asynchronous and moves onto the next line (the alert) without waiting for the window to load. You may not notice this as much on faster computers/simple pages. Even if it was synchronous, your code assumes item1 has already been set, but it's set from a button click.

                Comment

                • perhapscwk
                  New Member
                  • Sep 2007
                  • 123

                  #9
                  so how to pass back value from popup to primary window?

                  i use showmodaldialog ...it okay but not work for firefox 2..

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by perhapscwk
                    so how to pass back value from popup to primary window?
                    That part works. It's just that you're trying to alert it too quickly. If, for example, you use a button to alert it in the parent window and click it after you click the pop-up window button, it should work.

                    Originally posted by perhapscwk
                    i use showmodaldialog ...it okay but not work for firefox 2..
                    showModalDialog support was added in Firefox 3. If you want to support older browsers, either use a normal window.open or write your own pseudo-modal dialog.

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      yes ... in case you use a normal window.open() you need to implement a mechanism that 'synchronizes' those async operations you trigger. how to do that best, depends a bit on your needs, or better, needed flow of your code.

                      kind regards

                      Comment

                      Working...