js Dynamically generates Html elements to implement Post operations (createElement)

  • 2020-08-22 21:55:14
  • OfStack

Sometimes, if you need Post data on another page, you need to build an Form form


<form id="postform" name="postform" method="post">
<input name="msg" value=""/>
</form>


document.write("<form ..."
//document.write("<iframe src=\"about:blank\" name=\"hiddenFrame\" id=\"hiddenFrame\" width=\"0\" height=\"0\" frameborder=\"0\"></iframe>");

Submission with js as follows does not work because the form typed to the page is not an object, but a string


//  theForm.action = "http://msg.baihe.com/tortoise/pages/tortoise/sm_gb2312.jsp?ReturnURL="+strReturnURL;
//  document.getElementById("Pathid").value="3070";
//  document.getElementById("Title").value=" Hello! ";
//  document.getElementById("Content").value=" I put you in the spotlight, let's talk :) ";
//  document.getElementById("CloseWindow").value="1";

So you need to create your own form object dynamically, using the following method:


var form_feedback = document.createElement("form");
  document.body.appendChild(form_feedback);
    
  var i = document.createElement("input");
  i.type = "hidden";
  i.name = "Title";
  i.value = " Hello! ";
  form_feedback.appendChild(i);
  
  
  var j=document.createElement("input");
  j.type="hidden";
  j.name="Content";
  j.value=" I put you in the spotlight, let's talk :) ";
  form_feedback.appendChild(j);
  
  var hiddenIframe=document.createElement("iframe");
  hiddenIframe.src="about:blank";
  hiddenIframe.name="hiddenFrame";
  hiddenIframe.id="hiddenFrame";
  hiddenIframe.width="0";
  hiddenIframe.height="0";
  hiddenIframe.frameborder="0";
  form_feedback.appendChild(hiddenIframe);
  
  
  form_feedback.action = "http://msg.baihe.com/tortoise/pages/tortoise/sm_gb2312.jsp?ReturnURL=";
  form_feedback.target = "hiddenFrame";
  form_feedback.method = "post";
  form_feedback.submit();

Related articles: