打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[JavaScript]window.open的post與get傳遞參數比較

最近剛好有這樣的需求...小弟測試心得分享給大家呀...

一般利用js的window.open開另一網要傳參數都用Get..方式..在url後面接參數...

但如果傳帳號.密碼..可能會被看到...不安全..去找了一下window.open在post上的用法...

JScript.js

  1. function openWindowWithPost(url, name, keys, values) {   
  2.     var newWindow = window.open(url, name);   
  3.     if (!newWindow) return false;   
  4.     var html = "";   
  5.     html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";   
  6.     if (keys && values && (keys.length == values.length))   
  7.         for (var i = 0; i < keys.length; i++)   
  8.         html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";   
  9.     html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";   
  10.     newWindow.document.write(html);   
  11.     return newWindow;   
  12. }  

 

OpenPost.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="OpenPost.aspx.cs" Inherits="OpenPost" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.   
  7.     <script src="JScript.js" type="text/javascript"></script>  
  8.   
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.         <input id="id" type="text" />  
  15.   
  16.         <input id="pwd" type="password" />  
  17.   
  18.         <input id="Button1" type="button" value="Post" onclick="Post();" />  
  19.         <input id="Button2" type="button" value="Get" onclick="Get();" />  
  20.     </div>  
  21.     </form>  
  22. </body>  
  23. </html>  
  24.   
  25. <script type="text/javascript">  
  26.     function Post() {   
  27.         var keys = [];   
  28.         var values = [];   
  29.         keys[0] = "id";   
  30.         keys[1] = "pwd";   
  31.         values[0] = document.getElementById("id").value;   
  32.         values[1] = document.getElementById("pwd").value;   
  33.         openWindowWithPost("result.aspx", "web", keys, values);   
  34.     }   
  35.     function Get() {   
  36.         window.open("result.aspx?id=" + document.getElementById("id").value + "&pwd=" + document.getElementById("pwd").value, "web");   
  37.     }   
  38. </script>  

 

OpenPost.aspx.cs(省略)

result.aspx(省略)

result.aspx.cs

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Web;   
  4. using System.Web.UI;   
  5. using System.Web.UI.WebControls;   
  6.   
  7. public partial class result : System.Web.UI.Page   
  8. {   
  9.     protected void Page_Load(object sender, EventArgs e)   
  10.     {   
  11.         if (Request.Form["id"] != null)   
  12.         {   
  13.             Response.Write("id:" + Request.Form["id"].ToString() + "<" + "br/>");   
  14.         }   
  15.   
  16.         if (Request.Form["pwd"] != null)   
  17.         {   
  18.             Response.Write("pwd:" + Request.Form["pwd"].ToString() + "<" + "br/>");   
  19.         }   
  20.   
  21.         if (Request.QueryString["id"] != null)   
  22.         {   
  23.             Response.Write("id:" + Request.QueryString["id"].ToString() + "<" + "br/>");   
  24.         }   
  25.   
  26.         if (Request.QueryString["pwd"] != null)   
  27.         {   
  28.             Response.Write("pwd:" + Request.QueryString["pwd"].ToString() + "<" + "br/>");   
  29.         }   
  30.     }   
  31. }  

 

執行結果:

POST結果:

GET結果:

參考網址:

http://www.webdeveloper.com/forum/archive/index.php/t-1688.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Javascript访问html页面的控件的几种方法
E001 如何把文字内容写入Excel表格
JavaScript学习笔记(三)
javascript中控制类名className 属性
Javascript获取value值的三种方法及注意点
PHP HTML JavaScript MySQL代码如何互相传值的方法分享
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服