简体   繁体   中英

javascript : send actual html code to url

I have an array of HTML codes like this:

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

that I would like to send to URL as:

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?

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. 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 .

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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