Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Alexandre_T_
Beginner
60 Views

Intel XDK iOS app - Javascript - How to get "Get" variable in iframe

Hello,

I added an iframe (<iframe src="page.html?var1=xxx") for example on my Intel Xdk iOs app.

The page is well displayed, but GET variables don't work (work only in emulator, not after build)

 

Do you know how to get GET variables please ?

I used this code

    function $_GET() {
      // Get the Full href of the page e.g. http://www.google.com/files/script.php?v=1.8.7&country=india
      var href = window.location.href;
	
      // Get the protocol e.g. http
      var protocol = window.location.protocol + "//";
	
      // Get the host name e.g. www.google.com
      var hostname = window.location.hostname;
	
      // Get the pathname e.g. /files/script.php
      var pathname = window.location.pathname;
	
      // Remove protocol part
      var queries = href.replace(protocol, '');
	
      // Remove host part
      queries = queries.replace(hostname, '');
	
      // Remove pathname part
      queries = queries.replace(pathname, '');
	
      // Presently, what is left in the variable queries is : ?v=1.8.7&country=india
	
      // Perform query functions if present
      if (queries != "" && queries != "?") {
	
	    // Remove question mark ?
        queries = queries.slice(1);
	
        // Split all the different queries
        queries = queries.split("&");
		
        // Get the number of queries
        var length = queries.length;
		
        // Declare global variables to store keys and elements
        $_GET_Params = new Array();
        $_GET = {};
	
        // Perform functions per query
        for (var i  = 0; i < length; i++) {
			
          // Get the present query
          var key = queries;
			
          // Split the query and the value
          key = key.split("=");
			
          // Assign value to the $_GET variable
          $_GET[key[0]] = [key[1]];
			
          // Assign value to the $_GET_Params variable
          $_GET_Params = key[0];
        }
      }
    }

    // Execute the function
    $_GET();

<script>document.write($_GET['var1']);

 

Tags (2)
0 Kudos
1 Reply
Chris_P_Intel
Employee
60 Views

For starters, where is this script running? It should run from the page that is included _into_ the iframe, not its parent.

That's a lot work to get the query.  Try this:

function getQuery(name){
   var queryString = document.location.search; //?v=1.8.7&country=india
   queryString = queryString.substr(1); //drop ?
   var pairStrings = queryString.split("&"); //["v=1.8.7", "country=india"]
   var pairs = {};
   pairStrings.forEach(function(str){ 
     var pair = str.split("=");  // ["v", "1.8.7"]
     pairs[pair[0]] = pair[1];  // {v:"1.8.7"}
   });
   return pairs[name]; 
}

 

0 Kudos
Reply