PDA

View Full Version : Handling Special Characters in the URL



vishy
02-09-2009, 07:40 PM
Hi
In my application am trying to open a saved document from database. When i click on teh document name am passing the name of the document as the query string. The problem is when there isa %20 or # in the document name it keep throwing error. i tried encodeURI(documentname) and encodeURIcomponent(documentname). The later has support for handliong # but neither of them handles %20. As am using this in javascript i cant use java.net.URL class.
Please throw some light on this.

Thanks
Vishy

jscheuer1
02-09-2009, 09:02 PM
One shouldn't use spaces in filenames for live internet use. However, before you encode, you could do a replace:


documentname = documentname.replace(/%20/g, ' ');

But the space character will just get encoded again as %20 (which represents a space in an encoded URL), though that might just work out. I have no way to know for sure given what you've said so far.

If you want more help, it would probably help if I had a link to the problem page. Tell me what I have to do once I get there to see the problem.

vishy
02-09-2009, 09:09 PM
Below is my code:
function funOpen(documentId, documentName)
{
window.open('<%=contextPath%>/do/OpenDocs/' + encodeURIComponent(documentName) + '?method=openDocument&documentId=' + documentId);
}

It is suppose to open the saved cdocument as per the document id. I tried to open a file with the name: RE%20testfile#123.msg
It doesnt throw java error , but it is not opening the document instead this is what it shows in the URL:
https://servername:<<port>>/mydom/do/OpenDocs/RE%2520testfile%231879562.msg?method=openDocument&documentId=639

I dont know why 25 and a %23 is substituted for a whitespace and a # symbol.

jscheuer1
02-10-2009, 03:25 AM
The various %nn tokens are characters encoded as hex. If you are going to encode them again, they must first be decoded. I'd try this:


window.open('encodeURI(<%=contextPath%>/do/OpenDocs/' + unescape(documentName) + '?method=openDocument&documentId=' + unescape(documentId)));

You can always see what URL is being produced by changing from window.open to alert:


alert('encodeURI(<%=contextPath%>/do/OpenDocs/' + unescape(documentName) + '?method=openDocument&documentId=' + unescape(documentId)));

If it doesn't look like a valid URL, further adjustment will be required.