logo

Unescaping Domino's @URLEncode Function

Recently I learnt some page encoding lessons, which are probably worth mentioning. I was working on a JavaScript news ticker and certain characters were causing problems. Namely the £ sign.

Using a special view I was supplying the ticker's JavaScript function with an array of news items. To prevent problems with the code I decided to encode the news titles in the Domino view itself and then Unescape() them in the JavaScript. This is where the problems began.

Here's a demo of the problem in a simple database you can download (includes a free ticker).

In the column formula I used @URLEncode("Domino"; Title) to create the encoded text. This view is then embedded on a Page. The Page stores JavaScript and a function on it unescapes the news title. The problem is that the page is sent to the browser without a specific character set, which means that browsers assume your default charset. Unless this happens to be UTF-8, which is what @URLEncode("Domino") uses, you'll get problems when code on that Page unescapes anything. The unescape method will use the charset of the page on which the encoded strings reside. You have to make sure the encoded strings are on a page with the same charset.

One solution would be to specify the charset of the Page as UTF-8. This isn't possible though, as, with Domino, you can't specify both a charset and a content-type of "other" for a Page both at once. Sounds like a bug to me. You can specify the charset when you chose content type HTML. Just not when you chose other.

The solution I ended up using was to change the column formula to use @URLEncode("ISO-8859-1"; Title) instead. This fixed the issue on this UK-based web application, but might not be the solution for a global site. In this case we really need to tell the browser what charset to use and a better solution is needed.

Anyway, I thought it was worth passing on. If you find yourself using @URLEncode() along with Unescape() you'll need to take character sets in to account.

This weekend I am going back to Scotland to ride the three Staines we didn't manage last time. Wish me luck.

Comments

  1. I also think, it is a bug.

    As you obviously use "json/x-javascript" as ContentType, you are making XmlHttpRequests, aren't you?

    Don't know, if this will function properly, but give it a try:

    Set ContentType to HTML in the page and call overrideMimeType() on the XHR object before sending it.

    See

    {Link}

    for reference.

    Ciao

    Thomas

    • avatar
    • Jake Howlett
    • Thu 21 Sep 2006 07:58 AM

    Hi Thomas. That would work for Ajax, but I'm just using content-type application/x-javascript and embedding the view on a Page called "ticker.js", so it wouldn't work for me.

  2. As nice as escape() and unescape() are, Jake, I think you'll find that encodeURIComponent() and decodeURIComponent() are better matches for what you are trying to accomplish. encodeURIComponent() matches @URLEncode; you'd need to tag on a replace(/\+/g," ") to match @URLDecode("Domino",x) with decodeURIComponent.

  3. Sometimes domino sends charset us-ascii when displaying certain design elements

    (even if you have UTF-8 for output configured in the server doc/internet site).

    I have noticed this behaviour for ?OpenPage and ?ReadForm but haven't done any further investigations about exactly why.

    • avatar
    • Jake Howlett
    • Thu 21 Sep 2006 08:18 AM

    Stan. You're a genius.

    • avatar
    • Richard
    • Thu 21 Sep 2006 08:33 AM

    Jake, what makes Stan's post even more incredible is if you read his blog and see where he's posting from. He's a great guy!

    • avatar
    • Jake Howlett
    • Thu 21 Sep 2006 08:55 AM

    Agreed Richard. He never stops surprsing me, that man.

    • avatar
    • Vecnix
    • Thu 21 Sep 2006 04:07 PM

    aha Jake I am happy you resume you're technical articles , if you need help installing on a fedora core 5 someday I might just help , I was happy to read about your DIY and plasterer housing and stuff , but I am thinking back about my domino career ( I got the lost job man you said we're lucky I was not).

    I got diagnosed for something I don't know my manager told me , we don't have manager here , we don't design software , I got to stay home paid for sometimes then I got fired by a new guy they hired , they moved into business object and totally didn't give a chance to Domino.

    The fuck, I got an offer to work in algeria but it's been a while I didn't design anything. I started do php with cbts.

    The fuck with that I should have warned you before my last trip to england when you were doing the charity thing for the template , probably wouldn't have used it was .

    My Question is :

    Have you ever used the International function of Rnext err sorry I am old school now lol

    R6. I didn't get the training or the doc. I am the worst domino admin ever :-/ couldn't setup antirelay on my linux server :-/

    Well you're on my personalised google webpage

    If you have some time have look at Ajax website 43things.com

    • avatar
    • Tom
    • Thu 21 Sep 2006 09:06 PM

    hya sorry jake for bothering, do you think we will have an open beta for Hannover? Something I could get on with , I never touched eclipse. Did you?

    sorry for advertising urls here :x

    • avatar
    • IanB
    • Fri 22 Sep 2006 02:37 AM

    Jake

    Just got back from a weekend at Glentress near Peebles. To go back 20 years - man that place rocks :-)

    I might be the wrong side of 40 but even I had a play on the freeride course - the big box at the bottom IS rideable, I've got the video to prove it!

    Hope you have a great time whichever of the 7 you ride.

    Ian

    • avatar
    • larry
    • Fri 22 Sep 2006 06:31 AM

    Did anyone notice that www.notestips.com has been off-line for a while. Does anyone know why?

  4. @ larry

    Yes, but I've heard only rumors. I havn't seen or heard from Mike Golding in quite a while. Not sure if he's alive or dead at this point, or just retired. Or, maybe like Stan quietly recovering.

    @ Jake

    I expect you'll update Jake's Rides after the weekend? ;-)

  5. Mike's still around -- his photo blog is still up to date:

    {Link}

    Notestips.com, though, has kind of fallen by the wayside. A matter of priorities, I guess -- it takes effort to keep updating a site, and photography has a much higher leisure-time priority level for Mike nowadays.

    • avatar
    • Jake Howlett
    • Sun 24 Sep 2006 02:44 PM

    Ian. We were at Glentress Friday and Saturday (stayed in the motel at the entrance). I rode the box too! ;o) Most of the guys I was with are 40+ and led the pack most of the way!

    • avatar
    • Ketan
    • Mon 25 Sep 2006 10:51 PM

    Have you tried using "application/x-javascript; charset=UTF-8" as the content-type?

    • avatar
    • Jake Howlett
    • Tue 26 Sep 2006 03:59 AM

    Hi Ketan. An inspired idea, but it doesn't work, sadly. Although it is sent to the browser, the browser doesn't act on it. I think Stan's approach is still best.

    • avatar
    • Kerr
    • Tue 26 Sep 2006 04:22 PM

    I've just had a play around with a page, some xml and various charset settings.

    As far as I can tell, if you select a content-type of "other" then Jake you are correct, Domino does not sent the charset to the browser in the http header. Interesting to find that out. It does however alter how domino encodes the page. As Ketan suggested, if you put the charset after the content type it is sent to the browser, so you could have a mismatch between the charset specified and the actual encoding.

    So you should always specify the charset correctly after the content type and in the charset select box. Also note that if you specify the charset in the an xml preamble then the browser will use that, but not in preference to any charset in the http header. So if you are spitting out xml and put a charset in the preamble you don't need to explicitly put it in the content type field, but you should match what you specify in the charset select box.

    On to the subject of your original post Jake. The @urlencode will simply do a straight swap of the char specified to the hex code point or the target charset. (with % chars in front of each 2*hex byte.) Once that conversion has been done, domino will then encode it again along with everything else on the page into the target charset specified in the page properties and set it to the browser. The browser will then decode that using the charset specified, in the http header or the xml preamble or wherever it tries to work it out from. Only after that's been decoded will it get as far as being passed to any javascript decoding method you want to try. As Stan pointed out you were basically trying to decode a string using a method that didn't match your encoding method. Nothing really to do with the charsets used in sending it from domino to the browser and only related to the charsets in the encode/decode methods by accident. unescape() can accept %xx for ISO-8859-1 or %uxxxx for Unicode, so your workaround worked because you found a place where both the output from @urlencode matched the input for unescape().

    This is all 20/20 hindsight with a little digging and playing by the way. Thanks for the leads from yourself Jake and Stan and Ketan. Just thought I'd put down my conclusions as I was still left asking questions at the end.

    Final point of clarification. While it might be nice if Domino appended the correct charset to the end of the content type for you when you select content type other, Domino is not doing anything wrong and it does allow you to specify everything you need to. I'll leave it to IBM to claim that it's not a bug and is functioning as designed. I think I'd agree with them, as long as it was well documented so you knew what was going on. What you enter in that field is sent straight to the browser as the HTTP Content-type header. You tell Domino what charset to encode with using the select box. I can live with that.

    Golly a 546 word reply. Too verbose?

  6. Hello Guys,

    This solution really halped me a lot.I was having problrms about displaying the £ sign on web.It was displaying it as #.So I used this @ function mentioned herre and then made an unescape for my javascript variable.It worked. @Text(@URLEncode("ISO-8859-1"; "£")).

    Thanks a lot.

    • avatar
    • YoGi
    • Fri 9 Nov 2007 08:50 AM

    Those formulas tricks are quite good, but it doesn't work when it comes to manage long strings (> 2050 characters) ; you may experience a "Quoted string is too long" error.

    The only way I found is to use a java script library using java.net.URLDecoder.decode("long string", "UTF-8") with Domino R7+, or org.apache.commons.codec.net.URLCodec with Domino R6.

    More details in french here : {Link}

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 21 Sep 2006

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

Elsewhere

Here are the external links posted on the same day.

More links are available in the archive »

More Content