如何正确使用 Javascript 或 jQuery 对以下 URL 进行编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4201766/
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-08-23 11:22:14  来源:igfitidea点击:

How to encode the following URL using Javascript or jQuery properly?

javascriptjquery

提问by Mark K

All I want to do is encode the following link properly, but for some reason "#" is giving me a problem:

我想要做的就是正确编码以下链接,但由于某种原因“#”给了我一个问题:

var text = "hello, how are you? &am fine"
var link = "http://example.com/test#zzzzzzzzz"


url = "http://twitter.com/share?url=" + link + "&text" + text;
$("#twitter a").attr("href", url)

I tried encodeURIor encodeURIComponent, but still have issue with "#". If I manually replace "#" with "%23", then for some reason the code is encoded again. Does the jQuery attr()preform any encoding at all?

我试过encodeURIor encodeURIComponent,但仍然有“#”的问题。如果我用“%23”手动替换“#”,那么由于某种原因,代码会再次编码。jQuery 是否执行attr()任何编码?

EDIT Trying escape produces

编辑尝试逃生产生

http://twitter.com/share?url=http%253A//example.com/test%2523zzzzzzzz

Not sure where the "%25" is coming from rather than just %23

不确定“%25”是从哪里来的,而不仅仅是 %23

Using encodeURIComponentgenerates the following after doing $("#twitter a").attr("href", url). Where is the %25 is coming from?

encodeURIComponent执行后使用生成以下内容$("#twitter a").attr("href", url)。%25 来自哪里?

http://twitter.com/share?url=http%253A%252F%252Fexample.com%252Ftest%2523zzzzzzzz

回答by Jacob Relkin

encodeURIComponentshould work for you:

encodeURIComponent应该适合你

var text = "hello, how are you? & fine";
var link = "http://example.com/test#zzzzzzzzz";

var url = "http://twitter.com/share?url=" + encodeURIComponent(link) + "&text=" + encodeURIComponent(text);

$('#twitter a').attr('href', url);

jsFiddle example

jsFiddle 示例

回答by Xartok

encodeURIComponent is not "complete", you can use a custom function like this (taken from http://phpjs.org/functions/urlencode/) :

encodeURIComponent 不是“完整的”,您可以使用这样的自定义函数(取自http://phpjs.org/functions/urlencode/):

function encode(toEncode) {
    return encodeURIComponent(toEncode)
        .replace(/!/g, '%21')
        .replace(/'/g, '%27')
        .replace(/\(/g, '%28')
        .replace(/\)/g, '%29')
        .replace(/\*/g, '%2A');
}

Example : var url = encode(url_plain_text);

示例:var url = encode(url_plain_text);

回答by Praveen04

encodeURIComponentworks fine for me. we can give the url like this in ajax call.The code shown below :

encodeURIComponent对我来说很好用。我们可以在ajax调用中给出这样的url。代码如下:

$.ajax({
            cache: false,
            type: "POST",
            url: "http://atandra.mivamerchantdev.com//mm5/json.mvc?Store_Code=ATA&Function=Module&Module_Code=thub_connector&Module_Function=THUB_Request",
            data: "strChannelName=" + $('#txtupdstorename').val() + "&ServiceUrl=" + encodeURIComponent($('#txtupdserviceurl').val()),
            dataType: "HTML",
            success: function (data) {
            },
            error: function (xhr, ajaxOptions, thrownError) {
            }
        });

回答by Domenic

According to Firebug:

根据萤火虫的说法:

>>> encodeURIComponent("http://example.com/test#zzzzzzzzz")
"http%3A%2F%2Fexample.com%2Ftest%23zzzzzzzzz"

What issues are you having with the '#'?

您对“#”有什么问题?