XWidgetSoft Forum
https://www.bbs.xwidget.com/

Wikipedia search doesn't work when using spaces - solved
https://www.bbs.xwidget.com/viewtopic.php?f=8&t=2146
Page 1 of 1

Author:  Dagedee [ September 20th, 2012, 12:50 pm ]
Post subject:  Wikipedia search doesn't work when using spaces - solved

Hello,
I'm working on a Wikipedia search widget which works fine when using single keywords. But if using several keywords e.g. "Charlie Chaplin" with spaces between the words I'm getting an error message from Wikipedia (Charlie%20Chaplin).
It would be great if someone could help to fix that up!
Attachment:
single keyword.png
single keyword.png [ 9.08 KiB | Viewed 3467 times ]
Attachment:
keywords with space.png
keywords with space.png [ 14.01 KiB | Viewed 3467 times ]

Attachment:
wikisearch.rar [3.58 KiB]
Downloaded 332 times

Author:  Sooner266 [ October 5th, 2012, 12:17 am ]
Post subject:  Re: Wikipedia search doesn't work correctly when using space

You'll need to end up with + characters in the url where spaces should be. Someone may know of an easier way to do this, but I got it to work by using this:

var url = "http://www.wikipedia.org/w/index.php?search=" + encodeURIComponent(edit1.text).replace( /%20/g , "+" ) + "&ie=UTF-8&oe=UTF-8";

A regular .replace( "%20" , "+" ) only wanted to fix the first space, so a global regex replacement works better.
You may have to replace other punctuation too. Look up how Wikipedia reads its URLs and see how other people are encoding it in Javascript and you'll probably find an easy way to do it.

Author:  Dagedee [ October 5th, 2012, 10:15 am ]
Post subject:  Re: Wikipedia search doesn't work correctly when using space

Thank you very much for your help & hints - now it works perfectly !!! :D

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/