我有一个奇怪的情况,我试图使用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&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=7becomes
http://www.myurl.com/product-name?itemcolor=2&size=7Any free text should be run through nlapEscapeXML
nlapiEscapeXML('http://www.myurl.com/product-name?itemcolor=2&size=7');更多推荐
发布评论