#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    23
    Rep Power
    0

    Pass variables through ifame


    After seeing several examples of how to pass variables through to an iframe page, and after talking to various people at various coding sites, I STILL can't adopt their techniques to do it with my variables.

    So I think I need to be more clear:

    - My site is for playtesting a boardgame
    - There are 6 forms, 1 for each player
    - Each form has dropdown boxes and checkboxes
    - I have other forms on the page too, to deal with other things, but lets just focus on the player forms
    - when you click on a gameboard square, a page loads in an iframe. This page NEEDS to show variables from the player forms such as who owns the property, how many upgrades (checkboxes) are checked (think monopoly), etc..

    Everything in my script is now working except that I cannot pass these variables through.

    Someone said to do this:
    Code:
    <!------------South Africa----------->
    					if (window.parent.document.africa.south_africa_1.selectedIndex  == 0)
    					south_africa_owner = "--";
    					else if (window.parent.document.africa.south_africa_1.selectedIndex  == 1)
    					south_africa_owner = "1";
    					else if (window.parent.document.africa.south_africa_1.selectedIndex  == 2)
    					south_africa_owner = "2";
    					else if (window.parent.document.africa.south_africa_1.selectedIndex  == 3)
    					south_africa_owner = "3";
    					else if (window.parent.document.africa.south_africa_1.selectedIndex  == 4)
    					south_africa_owner = "4";
    					else if (window.parent.document.africa.south_africa_1.selectedIndex  == 5)
    					south_africa_owner = "5";
    					else
    					south_africa_owner = "6";
    					
    					if (window.parent.document.africa.south_africa_2.checked == true)
    						south_africa_mortgage = "MORTGAGED!";
    						else
    						south_africa_mortgage = "";
    					
    					if (window.parent.document.africa.south_africa_3.checked == true)
    						upgrade_1_1 = 1;
    						else
    						upgrade_1_1 = 0;
    						
    					if (window.parent.document.africa.south_africa_4.checked == true)
    						upgrade_1_2 = 1;
    						else
    						upgrade_1_2 = 0;
    						
    					if (window.parent.document.africa.south_africa_5.checked == true)
    						upgrade_1_3 = 1;
    						else
    						upgrade_1_3 = 0;
    						
    					if (window.parent.document.africa.south_africa_6.checked == true)
    						upgrade_1_4 = 1;
    						else
    						upgrade_1_4 = 0;
    						
    					if (window.parent.document.africa.south_africa_7.checked == true)
    						upgrade_1_5 = 1;
    						else
    						upgrade_1_5 = 0;
    						
    					south_africa_upgrades = (upgrade_1_1 + upgrade_1_2 + upgrade_1_3 + upgrade_1_4 + upgrade_1_5)
    ...adding window.parent

    I WISH it was as easy as putting window.parent in front of .document..., but that doesn't work.

    I'm not a professional coder, this isn't for money. It is an OFFLINE website used to playtest my boardgame. I've been working on it for years, and it is imperitive to me that I figure out how to pass these variables.

    Could someone PLEASE show me a clear example, and then help me out with anything in that example I don't understand?

    Thanks
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    I would pass the variables in a query string, to the iframe and then; have the page, that you have in the iframe, get the variables.

    Something like this:

    Code:
    <script>
    function getAndPass()
    {
    
    if (window.parent.document.africa.south_africa_1.selectedIndex  == 0)
    south_africa_owner = "--";
    else if (window.parent.document.africa.south_africa_1.selectedIndex  == 1)
    south_africa_owner = "1";
    else if (window.parent.document.africa.south_africa_1.selectedIndex  == 2)
    south_africa_owner = "2";
    else if (window.parent.document.africa.south_africa_1.selectedIndex  == 3)
    south_africa_owner = "3";
    else if (window.parent.document.africa.south_africa_1.selectedIndex  == 4)
    south_africa_owner = "4";
    else if (window.parent.document.africa.south_africa_1.selectedIndex  == 5)
    south_africa_owner = "5";
    else
    south_africa_owner = "6";
    
    if (window.parent.document.africa.south_africa_2.checked == true)
    	south_africa_mortgage = "MORTGAGED!";
    	else
    	south_africa_mortgage = "";
    
    if (window.parent.document.africa.south_africa_3.checked == true)
    	upgrade_1_1 = 1;
    	else
    	upgrade_1_1 = 0;
    	
    if (window.parent.document.africa.south_africa_4.checked == true)
    	upgrade_1_2 = 1;
    	else
    	upgrade_1_2 = 0;
    	
    if (window.parent.document.africa.south_africa_5.checked == true)
    	upgrade_1_3 = 1;
    	else
    	upgrade_1_3 = 0;
    	
    if (window.parent.document.africa.south_africa_6.checked == true)
    	upgrade_1_4 = 1;
    	else
    	upgrade_1_4 = 0;
    	
    if (window.parent.document.africa.south_africa_7.checked == true)
    	upgrade_1_5 = 1;
    	else
    	upgrade_1_5 = 0;
    	
    south_africa_upgrades = (upgrade_1_1 + upgrade_1_2 + upgrade_1_3 + upgrade_1_4 + upgrade_1_5)
    
    document.getElementById("results").src = "results_page.html?south_africa_owner=" + south_africa_owner + "&south_africa_mortgage=" + south_africa_mortgage + "&upgrade1=" + upgrade_1_1 + "&upgrade2=" + upgrade_1_2 + "&upgrade3=" + upgrade_1_3 + "&upgrade4=" + upgrade_1_4 + "&upgrade5=" + upgrade_1_5;
    
    }
    </script>
    
    <iframe id="results" src="results_page.html"></iframe>
    Then in the iframe page (ie "results_page.html"); do something like this:

    Code:
    <script>
    function getVariables()
    {
    var currentLocation = document.location.href;
    var sa_owner = currentLocation.split("south_africa_owner=")[1].split("&")[0]; // gets the south africa owner
    var sa_mortgage = currentLocation.split("south_africa_mortgage=")[1].split("&")[0]; // gets the south africa mortgage variable
    var upgrade_1 = currentLocation.split("upgrade1=")[1].split("&")[0]; // gets upgrade 1 variable
    var upgrade_2 = currentLocation.split("upgrade2=")[1].split("&")[0]; // gets upgrade 2 variable
    var upgrade_3 = currentLocation.split("upgrade3=")[1].split("&")[0]; // gets upgrade 3 variable
    var upgrade_4 = currentLocation.split("upgrade4=")[1].split("&")[0]; // gets upgrade 4 variable
    var upgrade_5 = currentLocation.split("upgrade5=")[1]; // gets upgrade 5 variable
    // then do whatever you want to do with these variables from there
    }
    document.onreadystatechange = function() {
     new getVariables();
    }
    </script>
    Last edited by web_loone08; October 21st, 2012 at 11:14 PM.

IMN logo majestic logo threadwatch logo seochat tools logo