在做一个音乐功能,需要往数据库存mp3的网络路径,比如:http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3这个URL显示的时候没问题,但用Javascript把把添加到一个<input type="text">中就有问题了,变乱码了!,从下面这张图可以看到:我想要的效果很简单,数据库里什么样,页面显示什么样,JS加到文本框也一样,烦恼各位帮帮忙,多谢!

解决方案 »

  1.   

    你页面什么编码  要采用UTF-8编码
      

  2.   

    试着用decodeURI() 把url解码后再放入input里
      

  3.   


    我页面utf-8,显示的url:http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3用decodeURI后,<input type="text">里:http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3
      

  4.   

    我发现在这里http://www.jonllen.com/jonllen/js/64.aspx可以用 decodeURIComponent() 方法把“http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3” 转换成 “http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3
    ”但我不行,很郁闷,页面都设置了编码的:
    <%@page language="java" import="java.util.*" pageEncoding="utf-8" session="false"%>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      

  5.   


    <script>
    alert(decodeURIComponent('11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3'))
    </script>
      

  6.   

    错行了,看这个
    <script>
    alert(decodeURIComponent('11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3'))
    </script>
      

  7.   

    刚才我单独写了个静态HTML:<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">
    var url = "http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3";
    alert(url);
    alert(decodeURIComponent(url));
    </script></head>
    <body></body>
    </html>
    效果:
    说明没问题,只是我现在通过Javascript方法把JSP页面的url传递到Javascript里,设置到<input type="text">就不行<a href="javascript:openPanel('<%=m.getId() %>','<%=m.getName() %>','<%=url %>','<%=m.getDiscription() %>','<%=createTime %>','<%=modfiyTimeStr %>','<%=m.getTypeId() %>')"><%=name %></a>//显示Music详细信息
    function openPanel(id,name,url,discription,createTime,modifyTime,musicTypeId){
    //http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3
    //http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3
    $("#musicDetailDiv").fadeIn(300);
    $("#BGDiv").fadeTo(900,0.1);
    $$$("musicId").value = id;
    $$$("musicName").value = name;
    alert(decodeURIComponent(url));
    $$$("muaicUrl").value = decodeURIComponent(url);
    $$$("muaicUrl").title = decodeURIComponent(url);
    $$$("musicDiscription").value = discription;
    $$$("createTimeSpan").innerHTML = createTime;
    $$$("modifyTimeSpan").innerHTML = modifyTime;
    $$$("musicTypeSelect2").value = musicTypeId;
    }结果:
      

  8.   


     在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。
    javaScript中的编码方法:
    escape() 方法:
    采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
    Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.
    encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character
    encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
    因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。       另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.
      

  9.   

    你JSP页面编码是UTF-8?
    <script type="text/javascript"  charset="UTF-8">
    都统一起来看看效果
      

  10.   


    后台:
    <%@ page import="java.net.URLDecoder"%>
    function openPanel(id,name,url,discription,createTime,modifyTime,musicTypeId){
        //http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3
        //http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3
        $("#musicDetailDiv").fadeIn(300);
        $("#BGDiv").fadeTo(900,0.1);
        $$$("musicId").value = id;
        $$$("musicName").value = name;
        alert(decodeURIComponent(url));
        $$$("muaicUrl").value = URLDecoder.decode(request.getParameter("param").getString(),"UTF-8"));
        $$$("muaicUrl").title = URLDecoder.decode(request.getParameter("param").getString(),"UTF-8"));
        $$$("musicDiscription").value = discription;
        $$$("createTimeSpan").innerHTML = createTime;
        $$$("modifyTimeSpan").innerHTML = modifyTime;
        $$$("musicTypeSelect2").value = musicTypeId;
    }
      

  11.   

    不是,我代码是这样的:都在JSP页面:
    编码都是utf-8:
    <%@page language="java" import="java.util.*" pageEncoding="utf-8" session="false"%><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <%
           List musicList = (List)request.getAttribute("musicList");
           if(musicList != null && musicList.size() > 0){
           for(int i = 0; i < musicList.size(); i++){
           Music m = (Music)musicList.get(i);
           String url = m.getUrl();
           if(url.length() > 60){
           //url = url.substring(0,45) + "...";
           }
           String name = m.getName();
           if(m.getIsDelete() == 2){
           name = "<span style='color: #8C8C8C'>" + name + "</span>";
           url = "<span style='color: #8C8C8C'>" + url + "</span>";
           }
           //url = URLDecoder.decode(url);
           //url = new String(url.getBytes("iso-8859-1"),"utf-8").trim();
           String createTime = m.getCreateTime().toString();
           createTime = formater.format(m.getCreateTime());
           String modfiyTimeStr = "";
           Date modifyTime = m.getModifyTime();
           if(modifyTime != null){
           modfiyTimeStr = formater.format(modifyTime);
           }
          %>
          <tr onmouseover="this.style.backgroundColor = '#E6EEFF'"  onmouseout="this.style.backgroundColor = 'transparent'">
            <td style="width: 30px; padding-left: 5px; padding-top: 5px;"><input type="checkbox" name="checkMusic" value="<%=m.getId() %>" /></td>
            <td style="width: 150px; padding-left: 5px; padding-top: 5px;"><a href="javascript:openPanel('<%=m.getId() %>','<%=m.getName() %>','<%=url %>','<%=m.getDiscription() %>','<%=createTime %>','<%=modfiyTimeStr %>','<%=m.getTypeId() %>')"><%=name %></a></td>
            <td style="width: 320px; padding-left: 5px; padding-top: 5px;" colspan="2" title="<%=m.getUrl() %>"><%=url %></td>
          </tr>
          <% }} %>
    Javascript
    <script type="text/javascript">
    //显示Music详细信息
    function openPanel(id,name,url,discription,createTime,modifyTime,musicTypeId){
    $("#musicDetailDiv").fadeIn(300);
    $("#BGDiv").fadeTo(900,0.1);
    $$$("musicId").value = id;
    $$$("musicName").value = name;
    alert(decodeURIComponent(url));
    $$$("muaicUrl").value = decodeURIComponent(url);
    $$$("muaicUrl").title = decodeURIComponent(url);
    $$$("musicDiscription").value = discription;
    $$$("createTimeSpan").innerHTML = createTime;
    $$$("modifyTimeSpan").innerHTML = modifyTime;
    $$$("musicTypeSelect2").value = musicTypeId;
    }
    </script>muaicUrl是个文本框:
    <input type="text" id="muaicUrl" name="muaicUrl" class="text" title="" />
    我怀疑是超链接方法<a href="javascript:openPanel('param1','param2','param3')">引起的乱码比如:参数param1是这种格式:http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3传进JS方法后就变成了:http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3
      

  12.   

    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">
    //var url = "http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3";
    function openPanel(url){
    alert(url);
    alert(decodeURIComponent(url));
    }
    </script></head>
    <body><a href="javascript:openPanel('http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3')">openPanel</a></body>
    </html>这个例子,转码前后都是乱码,真是超链接方法引起的
      

  13.   

    我昏头看错了,哈哈。
    还是要先在服务端想办法
    <%
              List musicList = (List)request.getAttribute("musicList");
              if(musicList != null && musicList.size() > 0){
                  for(int i = 0; i < musicList.size(); i++){
                      Music m = (Music)musicList.get(i);
                      String url = m.getUrl();                  
                      if(url.length() > 60){
                          //url = url.substring(0,45) + "...";
                      }
                      String name = m.getName();
                      if(m.getIsDelete() == 2){
                          name = "<span style='color: #8C8C8C'>" + name + "</span>";
                          url = "<span style='color: #8C8C8C'>" + url + "</span>";
                      }
                      //url = URLDecoder.decode(url);
                      //url = new String(url.getBytes("iso-8859-1"),"utf-8").trim();
                      url=URLDecoder.decode(url.getString(),"UTF-8")
                      String createTime = m.getCreateTime().toString();
                      createTime = formater.format(m.getCreateTime());
                      String modfiyTimeStr = "";
                      Date modifyTime = m.getModifyTime();
                      if(modifyTime != null){
                          modfiyTimeStr = formater.format(modifyTime);
                      }
          %>如果需要,把import="java.net.URLDecoder"加到顶部声明
      

  14.   

    针对13楼:
    那你加这个看看<script type="text/javascript"  charset="UTF-8">
    //var url = "http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3";
    function openPanel(url){
        alert(url);
        alert(decodeURIComponent(url));
    }
      

  15.   

    针对13楼例子,加上 charset="utf-8" 变成 :<script type="text/javascript" charset="utf-8"> 也不行,刚才一哥们说超链接不要那样传值,改成这样:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript" charset="utf-8">
    //var url = "http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3";
    function openPanel(url){
    alert(url);
    alert(decodeURIComponent(url));
    }
    </script></head>
    <body><a href="#" onclick="javascript:openPanel('http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3')">openPanel</a></html>
    </body>为什么href里调JS方法会乱码?不明白
      

  16.   

    16L:
    真是的,我没有顺着你的思路走,汗!!
    URL确实是这样的啊,要不escape什么的派什么用场呢
    <script type="text/javascript" charset="utf-8">
    //var url = "http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3";
    function openPanel(url){
        alert(url);
        alert(unescape(url));
        alert(decodeURIComponent(url));
    }
    </script>
    <a href="#" onclick="javascript:openPanel(escape('http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3'))">openPanel</a>
      

  17.   

    嗯,行了,你算是提供了两种方法:
    还是在href调用JS方法,不用onclick事件:第一,用Java事先把mp3的url全部解码,比如数据库这种格式:http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3
    ,解码成:http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3,超链接调用方法时不转码:<a href="javascript:openPanel('<%=m.getUrl() %>')">openPanel</a>在JS方法里转两次码就出来了数据库里的原始mp3 url路径:<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" charset="utf-8">
    function openPanel(url){
    alert(encodeURI(decodeURIComponent(url)));
    }
    </script></head>
    <body>
    <a href="javascript:openPanel('http://nethd.zhongsou.com/ttimg/i_109053/11654-变幻之风.mp3')">openPanel</a>
    </html>
    </body>
    第二:
    Java不转码,全部用Javascript解决:<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript" charset="utf-8">
    function openPanel(url){
    alert(encodeURI(decodeURIComponent(url)));
    }
    </script></head>
    <body>
    <a href="javascript:openPanel(escape('http://nethd.zhongsou.com/ttimg/i_109053/11654-%E5%8F%98%E5%B9%BB%E4%B9%8B%E9%A3%8E.mp3'))">openPanel</a>
    </html>
    </body>还有就是另一哥们说,<a href="#" onclick="openPanel('param')">测试</>,试了,确实可以,中间过程不需转码,但我href="#"有问题,程序会跳到首页,非常郁闷,所以这个方法就放弃了
      

  18.   

    解决了就好,值得高兴,呵呵
    --------------------------------------------
    关于【但我href="#"有问题,程序会跳到首页,非常郁闷】:你那样写是有问题,这样:
    <a href="#" onclick="void(0);openPanel('param')">测试</a>
      

  19.   

    不是,检查了好久,自动跳到首页是这样的,页面有个图片:<img id="statusImg" src="" />改成:<img id="statusImg" src="javascript:void(0)" /> 就Ok了