How to Convert Resource item (resx) with line breaks to JS variable ?
hello,
i'm using dictionary item, and i want to enable the customer to enter some text with line breaks. (customer use backoffice to edit the dictionary text, and hit enter when he wants to insert line break.) till here nothing special.
the problem is that i assign the dictionary item value to js variable, it causing js error, because it can't process correctly the linebreaks.
this code is NOT working (when the dictionary item contains line breaks):
var errName = "<xsl:value-of select='umbraco.library:GetDictionaryItem("error.name")' disable-output-escaping='yes' />";
i tried umbraco.library:ReplaceLineBreaks, xslt replace and js replace to replace the resx line breaks with "<br />" but with no help. (i think that the solution is to replace the linebreaks with br, but probably i'm doing it wrong..).
So the idea being to replace the line breaks by hand. Unfortunately I don't know which type of line breaks get saved with dictionary items, so you probably have to try out a bit, e.g. with \n\r.
Another way to go could be to render the text in a hidden <div> or <textarea>, and then setting your variable to the text contained in that html element. That way you wont have to worry about it.
Or you could UrlEncode the text in your xslt, and the UrlDecode it in your javascript when setting the variable.
thanks morten - i tried to use umbraco.library:urlEncode that works, but after that i needed to decode it in javascript. because as i understand there is no js function for that i use some decode function that i found on the web. still, with no success.
also, i tried the second method with the hidden div and with no success also.
i'm very frustrated, but i spent too much time on this, and i must move on. i hope to get back on that when i have more time because its somthing simple and it is very annoying.. (this is some enhancment that i want to do on my Contact Form Packages that i released).
Thanks for the help and if someone have more ideas i will love to here.
An XML parser must normalize all linefeeds into a single 
 character, regardless of platform - I don't know how the value from a Dictionary item is returned, but it's worth a try...
when i print the value of the $errorName, it displayed it without the linebreak. but also it display the $errorNameString without line break... it seems that it ignore the linebreak.
but when i try to put the value in the js variable, it breaks the code:
var errName = "<xsl:value-of select='$errorName' />";
I would still go with printing out the content to an html element, and then read it back from there. Could you post the code that you used when trying this? What was the problem with it? Should be pretty straight forward.
How to Convert Resource item (resx) with line breaks to JS variable ?
hello,
i'm using dictionary item, and i want to enable the customer to enter some text with line breaks.
(customer use backoffice to edit the dictionary text, and hit enter when he wants to insert line break.)
till here nothing special.
the problem is that i assign the dictionary item value to js variable, it causing js error, because it can't process correctly the linebreaks.
this code is NOT working (when the dictionary item contains line breaks):
i tried umbraco.library:ReplaceLineBreaks, xslt replace and js replace to replace the resx line breaks with "<br />" but with no help. (i think that the solution is to replace the linebreaks with br, but probably i'm doing it wrong..).
any suggestions?
Thanks!
Hi eran,
How about trying something like this:
So the idea being to replace the line breaks by hand. Unfortunately I don't know which type of line breaks get saved with dictionary items, so you probably have to try out a bit, e.g. with \n\r.
Hope that helps,
Sascha
Thanks for your time, but the problem is still exist. i can't find the type of line break that get saved in the dictionary item.
i tried using replace function:
with different combination, like: "\r\n", "\u", "\0d\0a", ".%0D%0A" and couple more, but with no success..
Thanks,
Eran.
Another way to go could be to render the text in a hidden <div> or <textarea>, and then setting your variable to the text contained in that html element. That way you wont have to worry about it.
Or you could UrlEncode the text in your xslt, and the UrlDecode it in your javascript when setting the variable.
thanks morten - i tried to use umbraco.library:urlEncode that works, but after that i needed to decode it in javascript. because as i understand there is no js function for that i use some decode function that i found on the web. still, with no success.
also, i tried the second method with the hidden div and with no success also.
i'm very frustrated, but i spent too much time on this, and i must move on. i hope to get back on that when i have more time because its somthing simple and it is very annoying.. (this is some enhancment that i want to do on my Contact Form Packages that i released).
Thanks for the help and if someone have more ideas i will love to here.
Eran.
Hi eran,
This could be a long shot, but since it's XML you should try this:
An XML parser must normalize all linefeeds into a single 
 character, regardless of platform - I don't know how the value from a Dictionary item is returned, but it's worth a try...
/Chriztian
@Chrizian, thanks, i declare new entity:
and also tried
and then:
when i print the value of the $errorName, it displayed it without the linebreak. but also it display the $errorNameString without line break...
it seems that it ignore the linebreak.
but when i try to put the value in the js variable, it breaks the code:
I would still go with printing out the content to an html element, and then read it back from there. Could you post the code that you used when trying this? What was the problem with it? Should be pretty straight forward.
i managed to do the line break cleaning.
i choose to develop xslt extenstion, that handle this.
this is the code in the xslt extension:
and in the xslt:
everything is tested and works, and the line breaks is removed (and i can even add other string manipulations).
Thanks,
Eran.
is working on a reply...