首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js获取现阶段url的中文参数

2012-10-29 
js获取当前url的中文参数问题源自于大傻同学的需求:A.html的参数传给B.html。必须是在B.html中用js代码来获

js获取当前url的中文参数

问题源自于大傻同学的需求:A.html的参数传给B.html。必须是在B.html中用js代码来获取该url中的中文参数。。。

网上的类似问题:http://bbs.chinaunix.net/viewthread.php?tid=887933 未解决。

正常情况下,我们在后台获取request传递过来的中文参数都比较好处理。但是这哥们,是直接从html页面跳转到另一个html页面中,然后当另一个html加载完后运行js来获取当前的url中的中文参数。实际上浏览器在把参数传递过来的时候,已不在是中文了,而是%2F%B0%D9%B6%C8%BF%D5%BC%E4格式的字符串了。即使url中显示的是中文,使用window.location.search.substring(1)获取的字段也是类似于%2F%B0%D9%B6%C8%BF%D5%BC%E4的字符串。

显然到这一步为止,我们希望有一个js自带的函数把%2F%B0%D9%B6%C8%BF%D5%BC%E4这样的字符串解析了就行了。

实际上,确实存在这样的一个函数。哈哈哈,这样问题就解决了。先对中文进行编码,然后解码。

通过解决这个问题还学了不少js的东西。

1.js控制页面跳转的方法:

Code:
  1. 第一种: <scriptlanguage="javascript">
  2. window.navigate("top.jsp"); </script>
  3. 第二种:
  4. <scriptlanguage="JavaScript"> self.location='top.htm';
  5. </script> 第三种:
  6. <scriptlanguage="javascript"type="text/javascript"> window.location.href="login.jsp?backurl="+window.location.href;
  7. </script>
  8. 第四种: <scriptlanguage="javascript">
  9. alert("返回上一页面"); window.history.back(-1);
  10. </script>
  11. 第五种: <scriptlanguage="javascript">
  12. alert("非法访问,请终止!"); top.location='xx.jsp';
  13. </script>

2.中文参数的编码和解码

http://www.w3school.com.cn/js/jsref_escape.asp

3.javascript解析url参数【把参数分离解析出来而已】

Code:
  1. <scripttype="text/javascript"> varLocString=String(window.document.location.href);
  2. functiongetQueryStr(str){
  3. varrs=newRegExp("(^|)"+str+"=([^/&]*)(/&|$)","gi").exec(LocString),tmp;
  4. if(tmp=rs){ returntmp[2];
  5. }
  6. //parametercannotbefound return"";
  7. }
  8. console.log(getQueryStr("parameter_name")); </script>

4.常识

 Jsp页面使用URL编码传递中文参数的情况下,在参数的解析过程中会出现乱码。由于java在设计的时候考虑到了国际化的问题,在java源程序编译成字节码的时候默认使用的是UTF-8编码。而在web运用上,由于不同的浏览器向服务器发送的信息采用的编码方式不同,在由像tomcat之类的服务器解码的时候会由于编码方式的不同而产生乱码,这是一个会困扰jsp初学者很久的问题。【http://www.360doc.com/content/10/0317/10/633992_19086741.shtml】下角同学问题的解决的Demo:A.html
Code:
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml">
  2. <head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  3. <title>test1</title>
  4. <scripttype="text/javascript">
  5. functionc(){
  6. vart=document.form1.t.value; console.log(t);
  7. console.log(escape(t)); console.log(unescape(t));
  8. self.location='xiajiao2.html?t='+escape(t);
  9. } </script>
  10. </head>
  11. <body><formid="form1"name="form1"action="xiajiao2.html"method="get">
  12. <inputtype="text"id="t"name="t"/><inputtype="button"onclick="returnc()"value="搜索"/>
  13. <br/>中国人
  14. </form></body>
  15. </html>

B.html

Code:
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml">
  2. <head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><scriptlanguage="javascript"type="text/javascript"src="static/login_scr/jquery-1.3.2.js"></script>
  3. <title>test2</title>
  4. <scripttype="text/javascript">
  5. varLocString=String(window.document.location.href); functiongetQueryStr(str){
  6. varrs=newRegExp("(^|)"+str+"=([^/&]*)(/&|$)","gi").exec(LocString); vartmp;
  7. if(tmp=rs){ returntmp[2];
  8. } return"";
  9. } $(document).ready(function(){
  10. console.log(unescape(getQueryStr("t"))); alert(unescape(getQueryStr("t")));
  11. //vart=window.location.search.substring(1);
  12. }); </script>
  13. </head>
  14. <body>中国人
  15. <!--<formid="form1"action="xiajiao2.html"method="get"><inputtype="submit"onclick="returncheck_register()"value="搜索"/>
  16. </form>--></body>
  17. </html>

热点排行