Results 1 to 4 of 4

Thread: Handling Special Characters in the URL

  1. #1
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Handling Special Characters in the URL

    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    One shouldn't use spaces in filenames for live internet use. However, before you encode, you could do a replace:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •