本教程操作环境:windows7系统、javascript1.8.5版、dell g3电脑。
在日常开发当中,我们可能遇到要将某个页面的参数通过url链接拼接的方式传递到另一个页面当中,在另一个页面当中进行使用,如果传输过去的是中文,那么可能会遇到中文乱码问题,那么该如何来解决呢?
<!--test01.html--><!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>title</title> <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script></head><body><p id="username">你好明天</p><p οnclick="send();">点击测试</p><script> function send(){ var url = "test02.html"; var username = $("#username").html();// window.open(encodeuri(url + "?username=" + username)); //encodeuri针对整个参数进行编码 window.open(url + "?username=" + encodeuricomponent(username)); //encodeuricomponent针对单个参数进行编码 }</script></body></html>
<!--test02--><!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>title</title> <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script></head><body><p id="username"></p><script> var urlinfo = window.location.href;//获取url var username = urlinfo.split("?")[1].split("=")[1];//拆分url得到”=”后面的参数// $("#username").html(decodeuri(username)); //decodeuri针对整个参数进行解码 $("#username").html(decodeuricomponent(username)); //decodeuricomponent针对单个参数进行解码// $("#username").html(username);</script></body></html>
针对中文乱码问题,最主要是通过(encodeuri,decodeuri),(encodeuricomponent,decodeuricomponent)两种方法进行参数的编码以及解码工作,其中xxxxuri最主要针对的是整个url参数,xxxxuricomponent针对的是单个url参数;
简单的分享就到这里,如有疑问,欢迎留言~
【推荐学习:javascript高级教程】
以上就是javascript中文url乱码怎么办的详细内容。
