Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:42:07+00:00 2026-05-29T12:42:07+00:00

Is there any valid use for javascript’s encodeURI function? As far as I can

  • 0

Is there any valid use for javascript’s encodeURI function?

As far as I can tell, when you are trying to make a HTTP request you should either have:

  • a complete URI
  • some fragment you want to put in a URI, which is either a unicode string or UTF-8 byte sequence

In the first case, obviously nothing needs to be done to request it. Note: if you actually want to pass it as a parameter (e.g ?url=http…) then you actually have an instance of the second case that happens to look like a URI.

In the second case, you should always convert a unicode string into UTF-8, and then call encodeURIComponent to escape all characters before adding it to a URI. (If you have a UTF-8 byte sequence instead of a unicode string you can skip the convert-to-utf8 step).

Assuming I havent missed anything, I can’t see a valid use for encodeURI. If you use it, it’s likely you’ve constructed an invalid URI and then attempted to “sanitize” it after the fact, which is simply not possible since you don’t know which characters were intended literally, and which were intended to be escaped.

I have seen a lot of advice against using escape(), but don’t see anybody discouraging encodeURI. Am I missing a valid use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:42:09+00:00Added an answer on May 29, 2026 at 12:42 pm

    I have a blog post which answers this question in a lot of detail.

    You should never use encodeURI to construct a URI programmatically, for the reasons you say — you should always use encodeURIComponent on the individual components, and then compose them into a complete URI.

    Where encodeURI is almost useful is in “cleaning” a URI, in accordance with Postel’s Law (“Be liberal in what you accept, and conservative in what you send.”) If someone gives you a complete URI, it may contain illegal characters, such as spaces, certain ASCII characters (such as double-quotes) and Unicode characters. encodeURI can be used to convert those illegal characters into legal percent-escaped sequences, without encoding delimiters. Similarly, decodeURI can be used to “pretty-print” a URI, showing percent-escaped sequences as technically-illegal bare characters.

    For example, the URL:

    http://example.com/admin/login?name=Helen Ødegård&gender=f
    

    is illegal, but it is still completely unambiguous. encodeURI converts it into the valid URI:

    http://example.com/admin/login?name=Helen%20%C3%98deg%C3%A5rd&gender=f
    

    An example of an application that might want to do this sort of “URI cleaning” is a web browser. When you type a URL into the address bar, it should attempt to convert any illegal characters into percent-escapes, rather than just having an error. Software that processes URIs (e.g., an HTML scraper that wants to get all the URLs in hyperlinks on a page) may also want to apply this kind of cleaning in case any of the URLs are technically illegal.

    Unfortunately, encodeURI has a critical flaw, which is that it escapes ‘%’ characters, making it completely useless for URI cleaning (it will double-escape any URI that already had percent-escapes). I have therefore borrowed Mozilla’s fixedEncodeURI function and improved it so that it correctly cleans URIs:

    function fixedEncodeURI(str) {
        return encodeURI(str).replace(/%25/g, '%').replace(/%5B/g, '[').replace(/%5D/g, ']');
    }
    

    So you should always use encodeURIComponent to construct URIs internally. You should only never use encodeURI, but you can use my fixedEncodeURI to attempt to “clean up” URIs that have been supplied from an external source (usually as part of a user interface).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any browser based XHTML editor which we can use to make text
Is there any tool that can parse a valid C program and generate a
Is there any valid use case for DataSet and DataTable now that we have
Is there any way to write a valid header/link/image? Something like <a href=index.html><h1 id=logo>Company
Is there any way to format this so it's a valid expression, without adding
Is there any cross-browser css sticky footer with W3C valid css and semantic X/HTML?
I know that technically all three ways below are valid, but is there any
Is there any query which can return me the number of revisions made to
Question: does operand order make any difference when evaluating equality or identity in JavaScript?
http://bassistance.de/jquery-plugins/jquery-plugin-validation/ looks to be the best jquery validation plugin out there. I can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.