JS 传递URL带中文的参数

JavaScript016

JS 传递URL带中文的参数,第1张

escape改成encodeURIComponent

// var url = "qreg.php?ss_bzn=" + escape(ss_bzn)

var url = "qreg.php?ss_bzn=" + encodeURIComponent(ss_bzn)

GBK与UTF-8的转码:

iconv("gbk","utf-8","php中文转码")//把中文gbk编码转为utf8

iconv("utf-8","gbk","php中文转码")//把中文utf8编码转为gbk

一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:

1.传参页面

Javascript代码:<script type=”text/javascript”>// <![CDATA[

function send(){

var url = "test01.html"

var userName = $("#userName").html()

window.open(encodeURI(url + "?userName=" + userName))}

// ]]>

</script>

2. 接收参数页面:test02.html

<script>

var urlinfo = window.location.href//获取url

var userName = urlinfo.split(“?”)[1].split(“=”)[1]//拆分url得到”=”後面的参数

$(“#userName”).html(decodeURI(userName))

</script>

二:如何获取Url“?”后,“=”的参数值:

A.首先用window.location.href获取到全部url值。

B.用split截取“?”后的全部

C.split(“?”)后面的[1]内数字,默认从0开始计算