JS-关于地址栏传中文参数乱码的解决办法

A.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
		//这是第一个页面a.html
		//通过地址栏传参的方式设置第二个页面b.html的标题为:中文
		var title = "中文";
		var _t = encodeURI(encodeURI(title));
		window.location.href = "b.html?title="+_t;
	</script>
</html>

  

B.html:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
		var _t = getQueryString("title"); //获取地址栏参数
		var title = decodeURI(_t); //只需要转一次码
		document.getElementsByTagName("title")[0].innerHTML = title;
		//获取地址栏参数的方法
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
	</script>
</html>

  

搬运来源:https://blog.csdn.net/sanai_1992/article/details/77336501
posted @ 2022-12-14 16:40  MiniDuck  阅读(278)  评论(0编辑  收藏  举报