title: How to dynamically load an iFrame source and autofill input forms with Javascript
body: <p>I need to retrieve URL parameters (which I can do successfully) and based on one parameter, decide which iframe src to fill, then with other parameters auto fill the form that is created via the form src.  First issue is that I can't keep the page from going into an infinite loop.  It loads properly and shows the right iframe, but the infinite loop (load) needs to stop.  Second, I can't get the other parameters to autofill the input values.</p>
  
  <p>Here is the code.  I hope you can help.  Here is the code </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-    xhtml1-200000126/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
  
  &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;!-- Keep your jQuery up to date --&gt;
  
  &lt;script  type="text/javascript"&gt;
  var urlParams;
  (window.onpopstate = function () {
  var match,
      pl     = /\+/g,  // Regex for replacing addition symbol with a space
      search = /([^&amp;=]+)=?([^&amp;]*)/g,
      decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
      query  = window.location.search.substring(1);
  
  urlParams = {};
  while (match = search.exec(query))
     urlParams[decode(match[1])] = decode(match[2]);
  })();
  
  
  var test = urlParams["entry"];
  var test2 = urlParams["test"];
  
  function iframedirect(){
  if (test=="sldk") {
      document.getElementById("frame1").src = "http://na-sj09.marketo.com/lp/cochlearsandbox/UpgradeInterest_IFrameLandingPage.html";
      var f1 = frames['frame1'].document.forms['mktForm_1108'];
      f1.elements['FirstName'].value = test;
  }else{
  document.getElementById("frame1").src = "http://na-sj09.marketo.com/lp/cochlearsandbox/CAM-UpgradeInterestForm_iFrameLandingPage2ndOption.html";
  }
  }
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body id="bodyId" class="mktEditable" align="center" &gt;
  &lt;iframe id="frame1" src="" onload="iframedirect()" height="750px" width="620px"  scrolling="no"    frameborder="0" marginwidth="0"&gt;&lt;/iframe&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>