Using JavaScript to send url input and control iframe before submitting?

I have a basic html setup:

        <input id="input_data" type="text" />
    <input id="submit_data" type="submit" value="submit" />
<br />
<iframe name="Frame1" id="Frame1" src="~/d/Test.php?DataId=(This is where I need input data)" ></iframe>

      

I looked at some similar headers and the methods didn't work. Perhaps I am doing something wrong as the iframe should not do anything until the button is clicked.

+3


source to share


2 answers


Try using a form

post and return tag to prevent page refresh. And install via dom. IFrame was loaded after adding src src



function add() {
  var url = document.getElementById('input_data').value
  document.getElementById('Frame1').src = "~/d/Test.php?DataId=" + url
  console.log(document.getElementById('Frame1').src);
  return false;
}
      

<form onsubmit="return add()">
  <input id="input_data" type="text" />
  <input id="submit_data" type="submit" value="submit" />
  <br />
</form>
<iframe name="Frame1" id="Frame1"></iframe>
      

Run codeHide result


+3


source


The iFrame is loaded as soon as the parent page is rendered because you are setting the src attribute on the html tag.

If you want to load it later, you can do so by setting the src attribute in JavaScript and then:



document.getElementById("Frame1").src = "/d/Test.php?DataId=" + data;

      

+2


source







All Articles