在JavaScript中使用encodeURIComponent来编码URL变量,但然后解码写入?(Using encodeURIComponent in JavaScript to encode U

编程入门 行业动态 更新时间:2024-10-27 06:23:18
在JavaScript中使用encodeURIComponent来编码URL变量,但然后解码写入?(Using encodeURIComponent in JavaScript to encode URL variable, but then decode to write?)

我有一个奇怪的情况,我试图使用JavaScript在XML文档中动态构建一个URL字符串。 我已经想出了如何使用encodeURIComponent在字符串中使用&符号来组装我的URL参数,并构建到我需要的URL。 但是,我需要将URL作为解码字符串写入页面,否则访问该URL不会执行我需要的操作。 整个文档使用JavaScript构建,因此它必须位于JavaScript环境中。

例如,为了使XML正确呈现,我可以组装下列URL:

http://www.myurl.com/product-name?itemcolor=2%26size=7

但是,我需要将其重新转换为XML文档中的链接:

http://www.myurl.com/product-name?itemcolor=2&size=7

那可能吗? 那有意义吗?

以下是我的代码片段:

var baseURL = 'http://myurl.com', urlappend = '', colorid, sizeid; function getVariants(item) { variants = '<variant>'; if ("custitem109" in item['columns']){ colorid = item['columns']['custitem109']['internalid']; urlappend += '?itemcolor=' + colorid; } if ("custitem110" in item['columns']){ sizeid = item['columns']['custitem110']['internalid']; urlappend += colorid ? encodeURIComponent('&') + 'size=' + sizeid : '?size=' + sizeid; } if (typeof item['columns']['urlcomponent'] !== 'undefined' && urlappend !== '') { variants += '<action_url>' + baseURL + '/' + item['columns']['urlcomponent'] + urlappend + '></action_url>'; } variants += '</variant>'; return variants; }

(这个函数被循环多次构造XML feed的一部分)

这是一个NetSuite suitelet,如果这有什么区别,或者如果任何人有任何问题,为什么这一切都必须在JS环境。

任何帮助提供将非常感激。

I've got a weird situation where I'm trying to dynamically construct a URL string in an XML document using JavaScript. I've figured out how to use encodeURIComponent to assemble my URL parameters using ampersands in a string and construct to the URL that I need. However, I need to write the URL to the page as a decoded string because otherwise, visiting that URL doesn't execute the actions I need to correctly. The whole document is constructed in JavaScript, so it has to be in a JavaScript environment.

For instance, I have the following URL that I'm able to assemble in order for the XML to render correctly:

http://www.myurl.com/product-name?itemcolor=2%26size=7

However, I need to turn it back into this as a link in the XML doc:

http://www.myurl.com/product-name?itemcolor=2&size=7

Is that possible? Does that make sense?

Here's my code snippet so far:

var baseURL = 'http://myurl.com', urlappend = '', colorid, sizeid; function getVariants(item) { variants = '<variant>'; if ("custitem109" in item['columns']){ colorid = item['columns']['custitem109']['internalid']; urlappend += '?itemcolor=' + colorid; } if ("custitem110" in item['columns']){ sizeid = item['columns']['custitem110']['internalid']; urlappend += colorid ? encodeURIComponent('&') + 'size=' + sizeid : '?size=' + sizeid; } if (typeof item['columns']['urlcomponent'] !== 'undefined' && urlappend !== '') { variants += '<action_url>' + baseURL + '/' + item['columns']['urlcomponent'] + urlappend + '></action_url>'; } variants += '</variant>'; return variants; }

(this function gets looped through multiple times to construct a portion of the XML feed)

This is a NetSuite suitelet if that makes any difference or if anyone has any question as to why this all has to be in a JS environment.

Any help provided would be very much appreciated.

最满意答案

其实你想要做的是正确构建你的URL,然后xml转义它们。

例如:

http://www.myurl.com/product-name?itemcolor=2&size=7

http://www.myurl.com/product-name?itemcolor=2&amp;size=7

任何免费的文本应该通过nlapEscapeXML运行

nlapiEscapeXML('http://www.myurl.com/product-name?itemcolor=2&size=7');

Actually what you want to do is construct your URLs properly and then xml escape them.

e.g:

http://www.myurl.com/product-name?itemcolor=2&size=7

becomes

http://www.myurl.com/product-name?itemcolor=2&amp;size=7

Any free text should be run through nlapEscapeXML

nlapiEscapeXML('http://www.myurl.com/product-name?itemcolor=2&size=7');

更多推荐

本文发布于:2023-07-15 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   URL   encodeURIComponent   JavaScript   write

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!