A html space is showing as instead of

前端 未结 5 630
自闭症患者
自闭症患者 2020-11-29 02:39

Passing a filename to the firefox browser causes it to replace spaces with %2520 instead of %20.

I have the following HTML in a file calle

相关标签:
5条回答
  • 2020-11-29 02:56

    When you are trying to visit a local filename through firefox browser, you have to force the file:\\\ protocol (http://en.wikipedia.org/wiki/File_URI_scheme) or else firefox will encode your space TWICE. Change the html snippet from this:

    <img src="C:\Documents and Settings\screenshots\Image01.png"/>
    

    to this:

    <img src="file:\\\C:\Documents and Settings\screenshots\Image01.png"/>
    

    or this:

    <img src="file://C:\Documents and Settings\screenshots\Image01.png"/>
    

    Then firefox is notified that this is a local filename, and it renders the image correctly in the browser, correctly encoding the string once.

    Helpful link: http://support.mozilla.org/en-US/questions/900466

    0 讨论(0)
  • 2020-11-29 03:01

    The following code snippet resolved my issue. Thought this might be useful to others.

    var strEnc = this.$.txtSearch.value.replace(/\s/g, "-");
    strEnc = strEnc.replace(/-/g, " ");

    Rather using default encodeURIComponent my first line of code is converting all spaces into hyphens using regex pattern /\s\g and the following line just does the reverse, i.e. converts all hyphens back to spaces using another regex pattern /-/g. Here /g is actually responsible for finding all matching characters.

    When I am sending this value to my Ajax call, it traverses as normal spaces or simply %20 and thus gets rid of double-encoding.

    0 讨论(0)
  • 2020-11-29 03:04

    Try this?

    encodeURIComponent('space word').replace(/%20/g,'+')

    0 讨论(0)
  • A bit of explaining as to what that %2520 is :

    The common space character is encoded as %20 as you noted yourself. The % character is encoded as %25.

    The way you get %2520 is when your url already has a %20 in it, and gets urlencoded again, which transforms the %20 to %2520.

    Are you (or any framework you might be using) double encoding characters?

    Edit: Expanding a bit on this, especially for LOCAL links. Assuming you want to link to the resource C:\my path\my file.html:

    • if you provide a local file path only, the browser is expected to encode and protect all characters given (in the above, you should give it with spaces as shown, since % is a valid filename character and as such it will be encoded) when converting to a proper URL (see next point).
    • if you provide a URL with the file:// protocol, you are basically stating that you have taken all precautions and encoded what needs encoding, the rest should be treated as special characters. In the above example, you should thus provide file:///c:/my%20path/my%20file.html. Aside from fixing slashes, clients should not encode characters here.

    NOTES:

    • Slash direction - forward slashes / are used in URLs, reverse slashes \ in Windows paths, but most clients will work with both by converting them to the proper forward slash.
    • In addition, there are 3 slashes after the protocol name, since you are silently referring to the current machine instead of a remote host ( the full unabbreviated path would be file://localhost/c:/my%20path/my%file.html ), but again most clients will work without the host part (ie two slashes only) by assuming you mean the local machine and adding the third slash.
    0 讨论(0)
  • 2020-11-29 03:23

    For some - possibly valid - reason the url was encoded twice. %25 is the urlencoded % sign. So the original url looked like:

    http://server.com/my path/
    

    Then it got urlencoded once:

    http://server.com/my%20path/
    

    and twice:

    http://server.com/my%2520path/
    

    So you should do no urlencoding - in your case - as other components seems to to that already for you. Use simply a space

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