Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to distinguish between escape, good encodeURI and encodeURIComponent

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to distinguish between escape, good encodeURI and encodeURIComponent. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

I. Preface

There are too many articles about the differences between these three methods, but most of them are very roundabout. This paper attempts to talk about these three methods from a practical point of view.

Escape and they are not in the same category.

Simply put, escape encodes strings (string) (while the other two are URL) to make them readable on all computers.

The effect after coding is in the form of% XX or% uXXXX.

ASCII letters, numbers, @ * / +, these characters will not be encoded, the rest will.

Most importantly, when you need to encode URL, forget this method, which is used for strings and does not apply to URL.

As a matter of fact, I haven't used this method in practical work, so I won't say much about it.

Third, the most commonly used encodeURI and encodeURIComponent

It is common to code URL, so these two methods should be paid special attention in practice.

They are all encoded URL, and the only difference is the range of characters encoded, where

The encodeURI method does not encode ASCII letters, numbers, ~! @ # $& * () =: /,; + 'for the following characters

The encodeURIComponent method does not encode ASCII letters, numbers, ~! * () 'for the following characters

So encodeURIComponent encodes a wider range than encodeURI.

For a practical example, encodeURIComponent encodes http:// as http%3A%2F%2F while encodeURI does not.

4. The most important thing is, what occasion and method should I use?

The difference between the above is very clear, let's talk about it from a practical example.

1. If it is just an encoded string and does not have anything to do with URL, then use escape.

2. If you need to encode the entire URL and then need to use this URL, use encodeURI.

such as

EncodeURI ("https://www.jb51.net/season-huang/some other thing")

After coding, it will become

"https://www.jb51.net/season-huang/some%20other%20thing";

Where the space is encoded as% 20. But if you use encodeURIComponent, the result becomes

"http%3A%2F%2Fwww.jb51.net%2Fseason-huang%2Fsome%20other%20thing"

See the difference? even the "/" is encoded, and the whole URL is no longer available.

3. When you need to encode the parameters in URL, then encodeURIComponent is the best way.

Var param = "https://www.jb51.net/season-huang/"; / / param is the parameter param = encodeURIComponent (param); var url =" https://www.jb51.net?next=" + param;console.log (url) / / https://www.jb51.net?next=http%3A%2F%2Fwww.jb51.net%2Fseason-huang%2F

As you can see, the "/" in the parameter can be encoded. If you use encodeURI, there will be a problem, because the following / needs to be encoded.

After reading the above, do you have any further understanding of how to distinguish between escape, good encodeURI and encodeURIComponent? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report