JS获取URL中参数,支持多参数传值,支持中文

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cplvfx/article/details/78653304

注意:这个只针对URL传参进行解读,如需看其他传参例子请点击链接转移!


名称:js实现页面与页面之间传值


url:http://blog.csdn.net/cplvfx/article/details/75009590


第一:页面代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>JS URL多参数传值,支持中文</title>
		<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
	</head>
	<body>
		<a href="index.html?vid=6&title=高温酷暑&level=一年级">开始传参</a>
		
		
		<div id="level">level</div>
		<div id="tit_h2">tit_h2</div> 
		
		
	<script type="text/javascript">
	function getUrlParam(name){
		//正则表达式过滤
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
		//正则规则
		console.log("reg==="+reg);
		//search:返回URL的查询部分
		console.log("location.search==="+location.search);
		//substr(1):从字符串第一个位置中提取一些字符
		console.log("location.search.substr(1)==="+location.search.substr(1));
		//match():在字符串内检索与正则表达式匹配的指定值,返回一个数组给r
		console.log("window.location.search.substr(1).match(reg)==="+window.location.search.substr(1).match(reg));
		var r = window.location.search.substr(1).match(reg); 
		//获取r数组中下标为2的值;(下标从0开始),用decodeURI()进行解码
		console.log("decodeURI(r[2])==="+decodeURI(r[2]));
		console.log("-----------------分隔符------------------------");
		if (r != null) return decodeURI(r[2]); return null; 
		
	}
	
	$(document).ready(function(){
		var vid = getUrlParam('vid');
		var title = getUrlParam('title');
		var level = getUrlParam('level');
		//对ID为tit_h2的标签赋值
		$("#tit_h2").html(title);
		$("#level").html(level);
		//id为plv_的标签用attr() 方法重新设置此id的值;例如:vid是1那么“plv_+vid”就是plv_1
		$("#plv_").attr("id","plv_"+vid); 
	});
</script>
	</body>
</html>


第二:知识点刨析


js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent


1 escape()函数

定义和用法 
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
语法 
escape(string)
参数  描述  
string  必需。要被转义或编码的字符串。 
返回值 
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。
说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。




2 encodeURI()函数 

定义和用法 
encodeURI() 函数可把字符串作为 URI 进行编码。
语法 
encodeURI(URIstring)
参数  描述  
URIstring  必需。一个字符串,含有 URI 或其他要编码的文本。 
返回值 
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。
说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#
 


3 encodeURIComponent() 函数

定义和用法 
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
语法 
encodeURIComponent(URIstring)
参数  描述  
URIstring  必需。一个字符串,含有 URI 组件或其他要编码的文本。 
返回值 
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。
说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。
提示和注释 
提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。
 

4 总结:

 通过对三个函数的分析,我们可以知道:escape()除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。而encodeURI() 用于编码整个URI,因为URI中的合法字符都不会被编码转换。encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的,它可以讲参数中的中文、特殊字符进行转义,而不会影响整个URL。




1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                            

例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a&gt;');</script>


2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");




3、   js使用数据时可以使用escape

例如:搜藏中history纪录。




4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)


escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z


encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z


encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z




示例(摘自W3School):

1 escape()

<script type="text/javascript">


document.write(escape("Visit W3School!") + "<br />")
document.write(escape("?!=()#%&"))


</script>
输出结果:
Visit%20W3School%21
%3F%21%3D%28%29%23%25%26




2 encodeURI()

<html>
<body>


<script type="text/javascript">


document.write(encodeURI("http://www.w3school.com.cn")+ "<br />")
document.write(encodeURI("http://www.w3school.com.cn/My first/")+ "<br />")
document.write(encodeURI(",/?:@&=+$#"))


</script>


</body>
</html>


输出结果:
http://www.w3school.com.cn
http://www.w3school.com.cn/My%20first/
,/?:@&=+$#
对整个URL进行编码,而URL的特定标识符不会被转码。


3  encodeURIComponent()

例1:
<script type="text/javascript">


document.write(encodeURIComponent("http://www.w3school.com.cn"))
document.write("<br />")
document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))


</script>
输出结果:
http%3A%2F%2Fwww.w3school.com.cn
http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23
对URL中的参数进行编码,因为参数也是一个URL,如果不编码会影响整个URL的跳转。


摘自:

作者:XingKong22star 

文章名称:js 中编码(encode)和解码(decode)的三种方法  

地址:http://blog.csdn.net/xingkong22star/article/details/39155739

附加延伸参考文献:

作者:cookie-niu

文章名称:JS对URL字符串进行编码/解码分析  

地址:http://blog.csdn.net/lishimin1012/article/details/52823528


橙说:这篇文章以百度URL为例,图文并茂进行解读,有兴趣的朋友可以去看下!























猜你喜欢

转载自blog.csdn.net/cplvfx/article/details/78653304