Unicode characters in a URL (all ok - except for IE)

前端 未结 1 1111
春和景丽
春和景丽 2021-01-21 06:25

I have some simple HTML which has a link to another HTML file - but that file name contains a unicode character. Depending upon how I encode the link, IE on Windows will not ope

相关标签:
1条回答
  • 2021-01-21 06:43

    This is discussed in the following IEBlog article on MSDN:

    File URIs in Windows

    Non US-ASCII Characters

    Characters outside of US-ASCII may appear in Windows file paths and accordingly they’re allowed in file IRIs. (URIs are defined as US-ASCII only and so when including non-US-ASCII characters in a string, what you've actually created is called an IRI: Internationalized Resource Identifier.) Don’t use percent-encoded octets to represent non US-ASCII characters because, in file URIs, percent-encoded octets are interpreted as a byte in the user’s current codepage. The meaning of a URI containing percent-encoded octets for bytes outside of US-ASCII will change depending on the locale in which the document is viewed. Instead, to represent a non-US-ASCII character you should use that character directly in the encoding of the document in which you are writing the IRI. For instance:

    Incorrect: file:///C:/example%E3%84%93.txt
    Correct: file:///C:/exampleㄓ.txt

    In other words, since your HTML is using UTF-8, the URL must use non-percent-encoded UTF-8 octets for the é character, which is why é.html works and %C3%A9.html fails - there is no file named é.html, for example.

    This is how Internet Explorer is designed to work. It is not a bug. The other browsers are simply doing something different, that's all. Unless you can configure the webserver to deliver different HTML to IE vs other browsers, you will have to use client-side technology instead, such as conditional comments, eg:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
    <title>website</title>
    </head>
    <body>
    
    <!--[if IE]>
    <a href="é.html">Works on IE</a>
    <![endif]-->
    <!--[if !IE]> -->
    <a href="%C3%A9.html">Works everywhere else</a>
    <!-- <![endif]-->
    
    </html>
    

    The X-UA-Compatible meta tag is needed because Microsoft removed support for HTML conditional comments in IE 10 when it implemented support for HTML5.

    0 讨论(0)
提交回复
热议问题