www.webdeveloper.com
Results 1 to 9 of 9

Thread: Passing a Variable from an IFrame to Parent Window

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    31

    Question Passing a Variable from an IFrame to Parent Window

    Hi everyone,

    I have a page which generates comboboxes dynamically inside IFrames.. Now when I select the combo inside the IFrame I want to pass the value back to my parent window... Ultimately I need to INSERT all the values into my database, but I can't INSERT the selections inside the IFRAME.

    So far I have written two javascript onchange functions which retrieve the values I need from within the IFRAME.. My OnChange Event shows a popup box with the correct variable... Now I have to get that variable to my parent window

    Any suggestions or help would be great!

    Thanks,
    Last edited by mattv10; 02-16-2007 at 08:58 AM.

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    Say you have the following variable in your iframe:
    Code:
    var myIFRAMEvar = 'Chocolate Chip Cookies';
    In that same script in the iframe, the next line would be:
    Code:
    window.top.myPARENTframeVar = myIFRAMEvar;
    So the whole thing will read:
    Code:
    var myIFRAMEvar = 'Chocolate Chip Cookies';
    window.top.myPARENTframeVar = myIFRAMEvar;
    Then in the parent frame of the iframe, you can access the copy of the iframe variable using myPARENTframeVar. Also, you can skip the whole process and grab the iframe variable from the parent frame using this, assuming you've named the IFRAME 'myIFRAME'.
    Code:
    alert(window.frames['myIFRAME'].myIFRAMEvar);

  3. #3
    Join Date
    Feb 2007
    Posts
    31
    I tried both methods. and each alert box returns undefined?? Am I doing something wrong?

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    That all depends on when you're trying to access variables. The parent document loads before the iframe document, so if you try to access the iframe document before it loads, then you get undefined. Can you post the full HTML and JavaScript being used?

  5. #5
    Join Date
    Feb 2007
    Posts
    31
    I'm not sure if this is exactly what I want to do.. Maybe you can throw some insight if you like after I explain. I have a form which has an IFRAME that dynamically populates a combobox based on a prior combobox selection. After I fill out the form I send all the values to a .asp page where I do REQUEST.FORM("value").ITEM....
    After I was messing around with your above example I was wondering if this was the best and only method to go because once I pass the IFRAME Values to the parent page I need to pass them to the INSERT Page.. Is there an easier way of doing this?

  6. #6
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  7. #7
    Join Date
    Feb 2007
    Posts
    31
    Ok here is my code.. I'm going to try to limit the amount of code so if you need more just ask...


    My Javascript Function in my parent.html (My Form)
    Code:
    function getIFrameVar(){    window.frames['jobFrame'].getJob();}
    My Javascript Function in my IFRAME (On the same Form)
    Code:
    function getJob(job){
    	var JOB = job;
    	alert(JOB);
    }
    My Onchange event that passes the IFRAME ComboBox Selection to the getJob Function
    Code:
    <select name="selJob" onChange='getJob(selJob.value)'>
    Now, just to test this I declared an onClick Event to a button on my parent page after my IFRAME would be generated to see if I successfully passed the value to my parent function.. However, it returns an alertbox that says "undefined"..

    If I can get this alertbox to show my value I want to pass the variable between these functions and submit them with my form when I hit the add button...

  8. #8
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  9. #9
    Join Date
    Jul 2014
    Posts
    1
    Beware there is an issue with the Chrome browser failing to implement basic javascript variable addressing between a webpage and embedded IFRAME pages.

    window.iframename.variable and parent.variable don't work as they should.

    Chrome javascript issue: No variable scope from IFRAME to parent frame

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles