techdoc centre
本站搜索 用户名: 密码:
   HOME  TECHDOC  WEB  SDK  NSDT  TOPIC  BIZDOC   LOGIN
  发布地区 Global 发布类别 NSDT Javascript
  Using Javascript to POST data between pages
于 2017-12-12 编辑: Eric 查看: 1100 次   Article Url Print this page

Posted by pcfinch on June 2, 2008

The following is a simple example of how to submit data from one HTML page to another using the POST method from Javascript. Normally, if data needs to be sent from one HTML page to another, it is done by appending the information to the query parameter of the the URL in the familiar “name=value” format. e.g.

< href="post.aspx?user=peter&cc=aus">Click</a>

Although this works fine, in most cases, problems occur when there is a lot of data to send and the URL exceeds about 2000 characters. The other disadvantage is that the URL looks ugly. The traditional method to get around this is to POST the data using a form. e.g.

<form name="myform" method="post" action="post.aspx">
<input name="user" value="peter"/>
<input value="cc" value="aus"/>
<a href="javascript:myform.submit()">Click</a>
<form>

This works fine, however, it does tend to make the formatting of the resulting HTML difficult and increases the size of the final HTML page significantly if there are lots of links on it.

The following javascript function takes the URL of the target page and an associative array of name/values paires and POSTs the data to the supplied URL by dynamically creating a form and then submitting it.

function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

To insert a link into a page just use a normal anchor tag in the HTML and call the function.

<a href="javascript:postwith('post.aspx',{user:'peter',cc:'aus'})">click</a>

This works on Microsoft IE, Mozilla Firefox and OS X Safari.

网址: 相关网址


相关文章:

o javascript判断浏览器类型与版本
o JavaScript 获取客户端计算机硬件及系统信息
o JavaScript使用技巧精萃
o 检查浏览器是否支持javascript和cookie
o js校验输入内容
o 动态增大,减小TEXTAREA框的高度
o JS验证中文长度
o 一个注册页面检查代码-JAVASCRIPT
o 使用PHP+javascript检查用户名是否存在
o Javascript中最常用的55个经典技巧


   Search:

   

   Categories:

 Open All Close


Copyright techdoc.netsoftlab.ca powered by netsoft lab. 2007-2012  
Friend Websites:   netsoftlab.ca   cnstar.ca   CS lessions