简体   繁体   English

javascript:将实际的 html 代码发送到 url

[英]javascript : send actual html code to url

I have an array of HTML codes like this:我有一个 HTML 代码数组,如下所示:

array = ['%', '&#85']

that I would like to send to URL as:我想发送给 URL 为:

url + '%,U' 

but when I write:但是当我写:

url + array

the result is:结果是:

url%,U

Is there a way or function to send the HTML code as it is?有没有办法或 function 按原样发送 HTML 代码?

You could probably just put your stringified array into the encodeURIComponent function, which takes care of all non-URL-save special chars and then use the reverse function decodeURIComponent (or its equivalent in other languages) to get the original data back as a string.您可能只需将您的字符串化数组放入encodeURIComponent function,它会处理所有非 URL 保存的特殊字符,然后使用反向 function decodeURIComponent (或其他语言中的等效项)将原始数据作为字符串返回。 So what you probably should do is所以你可能应该做的是

encoded = encodeURIComponent(JSON.stringify(array))
decoded = JSON.parse(decodeURIComponent(array))
myArray = ['aaa', 'bbb', 'ccc'];
var arrStr = encodeURIComponent(JSON.stringify(myArray));
$('#myLink').attr({ href: '/myLink?array=' + arrStr });

Convert array to a string first before appending it to the url .先将array转换为字符串,然后再将其附加到url

 var array = ['%', '&#85']; var url = 'foo.baz'; console.log(url + array.join(','));

This would help you.这会对你有所帮助。

 let array = ['%', '&#85']; let url = 'https://jsfiddle.net/'; console.log(url+array)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM