简体   繁体   English

URL中使用希腊字母作为参数

[英]Greek letters as parameters in URL

I have been struggling to pass greek letters as parameters in a URL (eg http://localhost/test.html?text=Καλημέρα ) 我一直在努力通过希腊字母作为URL中的参数(例如, http://localhost/test.html?text =Καλημέρα

For this reason I have created a simple html: 因此,我创建了一个简单的html:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">

<body>

<p id="text"></p>

<script type="text/javascript">
    function getQueryVariable(variable)
    {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
      }
      return(false);
    }
    var i = getQueryVariable("text");

    document.getElementById("text").innerHTML = i;

</script>

</body>
</html>

however when I open the site from a browser, javascript seems to fail to properly handle the input parameter and print it as Greek text 但是,当我从浏览器打开网站时,javascript似乎无法正确处理输入参数并将其打印为希腊文本

Here is what I get in the browser's window: 这是我在浏览器窗口中看到的内容:

%CE%9A%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1 %CE%9A%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1

instead of Καλημέρα. 而不是Καλημέρα。

Ι thought it was a matter of encoding that is why I plassed a UTF-8 statement at the top of the html. 我以为是编码问题,所以我在html的顶部插入了UTF-8语句。 Can you please advise me on how I should properly pass something "Greek" as url input? 您能否建议我如何正确地将“希腊语”作为URL输入?

You just need to take care of the URL decoding yourself in this instance. 在这种情况下,您只需要照顾自己解码的URL即可。

decodeURIComponent is what you should use for that: decodeURIComponent是您应该使用的:

if(pair[0] == variable){return decodeURIComponent(pair[1]);}

(If you only have Greek letters inside of the value, that should do. If the parameter name itself could have Greek letters as well, then you might want to decode pair[0] as well, before you compare it to the name stored in variable .) (如果值中仅包含希腊字母,则应该这样做。如果参数名称本身也可以包含希腊字母,则在将其与存储在其中的名称进行比较之前,可能还需要对pair[0]进行解码variable 。)

This docs with example like you question) 这个文档带有您喜欢的示例的问题)

Solution: 解:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">

<body>

<p id="text"></p>

<script type="text/javascript">
    function getQueryVariable(variable)
    {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return decodeURI(pair[1]);}
      }
      return(false);
    }
    var i = getQueryVariable("text");

    document.getElementById("text").innerHTML = i;

</script>

</body>
</html>

You can use the built in JS methods encodeURI and decodeURI for this, like so... 您可以为此使用内置的JS方法encodeURIdecodeURI ,如下所示...

var url = "http://somesite.com?q=Καλημέρα", 
    encoded = encodeURI(url),
    decoded = decodeURI(encoded);

console.log("encoded:", encoded);
//=> http://somesite.com?q=%CE%9A%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1
console.log("decoded:", decoded);
//=> http://somesite.com?q=Καλημέρα

Hope that helped :) 希望有帮助:)

encodeURI @ MDN encodeURI @ MDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM