不同web如何通讯之 html页面获取传递的参数

本文介绍了一种使用JavaScript解析URL中参数的方法。通过分析location.href获取当前页面的完整URL,并利用indexOf和substring方法截取参数字符串。进一步地,通过split将参数字符串拆分为数组,并遍历数组解析出每个参数名及其对应的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   有时候在项目开发过程中,因为要进行数据传递,或者进行重定向。如果是使用同一个平台,比如jsp,或者asp,那么文件非常好解决,但是我们在开发过程中,特别是具体接口功能的页面,就无法规定使用何种语言,

所有可以是用html进行转换,那么如何获取传递的参数,看下面的js。

<script language="JavaScript">
var pos,str,para,parastr,tempstr1;
tempstr="";
str = window.location.href;
pos = str.indexOf("?")
parastr = str.substring(pos+1);
document.write("<br>文件路径:"+str);
if (pos>0){
 document.write("<br>所有参数:"+parastr);
 }
else
 {
 document.write ("无参数");
 }

// 文件路径:hello.html?a=1&b=2&c=3
// 所有参数:a=1&b=2&c=3
// 参数0:a等于:1
// 参数1:b等于:2
// 参数2:c等于:3

if (str.indexOf("&")>0){
 para = parastr.split("&");
 for(i=0;i<para.length;i++)
 {
 tempstr1 = para[i];
 
 pos = tempstr1.indexOf("=");
 //document.write (tempstr1.substring(0,pos));
 document.write ("<br>参数"+i+":"+tempstr1.substring(0,pos));
 document.write ("等于:"+tempstr1.substring(pos+1));
 }
 }
</script>

未完,代完善

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值