javaScript中url中文参数(encodeURI函数和encodeURIComponent函数)

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>url中文参数</title>
<link rel="stylesheet" type="text/css" href="../css/body.css" />
<link rel="stylesheet" type="text/css" href="../css/mark.css" />
<link rel="stylesheet" type="text/css" href="../css/input2.css" />
<link rel="stylesheet" type="text/css" href="../css/console.css" />
</head>
<body>
<h1>url中文参数</h1>
<input type="button" id="input1" value="url中文参数(encodeURI函数和encodeURIComponent函数)" />
<br />
</body>
<script type="text/javascript">
document.getElementById('input1').onclick = function() {
//参考文章https://www.cnblogs.com/artwl/archive/2012/03/07/2382848.html

//encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
//encodeURI就是为这个而设计的。encodeURI不对URI中的特殊字符进行编码,如冒号(:)、斜杠(/)。
	console.log('中国', encodeURI('中国'), encodeURIComponent('中国'));
	console.log('中 国', encodeURI('中 国'), encodeURIComponent('中 国'));
	console.log('中#国', encodeURI('中#国'), encodeURIComponent('中#国'));
	console.log('中?国', encodeURI('中?国'), encodeURIComponent('中?国'));
	console.log('中@国', encodeURI('中@国'), encodeURIComponent('中@国'));
	console.log('中/国', encodeURI('中/国'), encodeURIComponent('中/国'));
	console.log('中&国', encodeURI('中&国'), encodeURIComponent('中&国'));
	console.log('江西省赣州市于都县', encodeURI('江西省赣州市于都县'));
	console.log('https://blog.csdn.net/czh500/hello hi test.html');
//encodeURI不对URI中的特殊字符进行编码,如冒号(:)、斜杠(/),仅仅把空格替换成了20%
//https://blog.csdn.net/czh500/hello%20hi%20test.html
	console.log(encodeURI('https://blog.csdn.net/czh500/hello hi test.html'));
	
var param = '于都县';
var url = "https://blog.csdn.net/czh500/?key=" + encodeURIComponent(param) + "&page=1";
console.log(url);
var url2 = "https://blog.csdn.net/czh500/?key=" + encodeURI(param) + "&page=1";
console.log(url2);


//encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
//encodeURIComponent方法对中文,空格,井号(#),斜线(/),冒号(:)都进行了编码
var p = 'http://www.baidu.com';
var url3 = 'https://blog.csdn.net/czh500/?url=' + p;
console.log(url3);
console.log(encodeURI(url3));
console.log(encodeURIComponent(url3));

console.log(encodeURI(p));
console.log(encodeURIComponent(p));
// window.location = url3;
// window.location = encodeURI(url3);
// window.location = encodeURIComponent(url3);
// window.location = p;
// window.location = encodeURI(p);
// window.location = encodeURIComponent(p);
console.log('江西于都县', encodeURI('江西于都县'), encodeURIComponent('江西于都县'));
console.log('http://www.baidu.com');
console.log(encodeURI('http://www.baidu.com'));
console.log(encodeURIComponent('http://www.baidu.com'));
}
</script>
</html>

其实,主要是讲了encodeURI函数encodeURIComponent函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值