javascript 如何知道 URL 是否已解码/编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17564837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:48:11  来源:igfitidea点击:

How to know if a URL is decoded/encoded?

javascripturlencodeurldecode

提问by Tapas Bose

I am using Javascript method decodeURIComponentto decode an encoded URL. Now I am having an issue, that sometimes the URL is get encoded twice during redirection between servers, sometimes it is encoded only once.

我正在使用 Javascript 方法decodeURIComponent来解码编码的 URL。现在我遇到了一个问题,有时 URL 在服务器之间重定向期间被编码两次,有时它只被编码一次。

I want to check that if the URL is still encoded after calling the method decodeURIComponent. How can I do that? Any pointer would be very helpful to me.

我想检查在调用方法后 URL 是否仍然被编码decodeURIComponent。我怎样才能做到这一点?任何指针都会对我很有帮助。

Update - 1

更新 - 1

If I recursively call a method and check that if the given URL still contains "%", if it contains "%" then decode it and call the method again; and if not return it to the caller, will that work?

如果我递归调用一个方法并检查给定的 URL 是否仍然包含“%”,如果它包含“%”,则对其进行解码并再次调用该方法;如果不将其返回给调用者,那行得通吗?

Update - 2

更新 - 2

For my case I have:

对于我的情况,我有:

callBackUrl=http%253A%252F%252Fadbc.com%252FPOSM%252Fapp%252Fpages%252Fadf.task-flow%253Fadf.tfDoc%253D%25252FWEB-INF%25252Ftask-flows%25252Fcatalog-edit-task-flow.xml%2526adf.tfId%253Dcatalog%2526_adf.ctrl-state%253Db9akorh22_9%2526articleReference%253D10C00135%2526previousView%253Dcatalog-home%2526fromUCM%253Dtrue%2526articleType%253Dposm%2526developer%253Dcentral

Now I am taking the value of the callBackUrl in my js method, then decoding it and firing window.open()with that decoded URL. the parameters are same and it has:

现在我在我的 js 方法中获取 callBackUrl 的值,然后对其进行解码并window.open()使用该解码后的 URL触发。参数是相同的,它有:

  • adf.tfDoc
  • adf.tfId
  • articleReference
  • previousView
  • fromUCM
  • articleType
  • developer
  • 文档
  • 文件名
  • 文章参考
  • 上一个视图
  • 来自UCM
  • 文章类型
  • 开发商

Parameters into it. So I know there is no query string like value="%..".

参数进去。所以我知道没有像value="%..".

Update - 3

更新 - 3

I have written the following method:

我写了以下方法:

var decodeURLRecursively = function(url) {
    if(url.indexOf('%') != -1) {
        return decodeURLRecursively(decodeURIComponent(url));
    }

    return url;
}

采纳答案by Niet the Dark Absol

Repeatedly decoding until you find no %signs will work over 99% of the time. It'll work even better if you repeatedly call so long as a match for /%[0-9a-f]{2}/ican be found.

重复解码直到你发现没有%迹象将在 99% 的时间内工作。如果您反复调用,只要/%[0-9a-f]{2}/i能找到匹配项,效果会更好。

However, if I were (for some bizarre reason) to name a file 100%achieved, that would cause a problem because %acwould be decoded to ?, causing the decode to fail. Unfortunately there's no way to detect this case.

但是,如果我(出于某种奇怪的原因)命名文件100%achieved,则会导致问题,因为%ac会被解码为?,从而导致解码失败。不幸的是,没有办法检测到这种情况。

Ideally you should know if something is encoded more than once, and optimally you shouldn't let it happen in the first place.

理想情况下,您应该知道某些内容是否被多次编码,并且最好一开始就不应该让它发生。

回答by rodu

There is a simple way to konw if a URL string is encoded.

有一种简单的方法可以知道 URL 字符串是否已编码。

Take the initial string and compare it with the result of decoding it. If the result is the same, the string is not encoded; if the result is different then it is encoded.

取初始字符串并将其与解码结果进行比较。如果结果相同,则不对该字符串进行编码;如果结果不同,则对其进行编码。

I had this issue with my urls and I used this functions:

我的网址有这个问题,我使用了这个功能:

function isEncoded(uri) {
  uri = uri || '';

  return uri !== decodeURIComponent(uri);
}

So now I can use isEncodedas the discriminant in a while loop (or with recursion) to know if I need to keep calling decodeURIComponenton the string:

所以现在我可以isEncoded在 while 循环(或递归)中用作判别式来知道我是否需要继续调用decodeURIComponent字符串:

function fullyDecodeURI(uri){

  while (isEncoded(uri)){
    uri = decodeURIComponent(uri);
  }

  return uri;
}

This solved the problem for me of decoding urls encoded multiple times. Hope this helps.

这为我解决了解码多次编码的 url 的问题。希望这可以帮助。

回答by Jimmy Huang

you can keep decode until the string did not change:

您可以继续解码,直到字符串没有改变:

str = "xxxxxxx"
dec_str = decode(str)
while(dec_str != str)
     str = dec_str;
     dec_str = decode(str);

回答by Carlos Silva

function checkEncodeURI(str) {
 return /\%/i.test(str)
}

Test :

测试 :

let url1 = "https://quora.com/unanswered/I'm-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I've-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one"
let url2 = 'https://www.quora.com/unanswered/I%E2%80%99m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I%E2%80%99ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one'
let a = checkEncodeURI(url1)
console.log(a)
let b = checkEncodeURI(url2)
console.log(b)