find a location for property in a new city

Thursday 18 October 2012

To encodeURI or to encodeURIComponent?

Solved a bug today whilst encoding in JavaScript a URL that contained a certain special character: the hash character #.

I found a generated querystring was not working correctly because many of the params were not being passed to the server somehow. I was generating a link like so: var link = 'http://www.britishdeveloper.co.uk?link=' + encodeURI(url) + '&userID=232';

I was quite pleased with myself remembering to URL Encode params that are going into a querystring to get around special characters but I had not thought of something... the hash character #!

Say the url being placed in the querystring is '/my page.aspx#comment3' my link was coming out as http://www.britishdeveloper.co.uk?link=/my%20page.aspx#comment3&userID=232.

What's wrong with that?

Well it's not what I was expecting really but it was working up until I got urls with hashes in. As you may or may not know everything proceeding a # in a url is for browsers so your server will ignore everything after it. In this case the userID param was not seen by my server.

encodeURIComponent not encodeURI

For this particular scenario encodeURIComponent was what I needed since:

encodeURI('/my page.aspx#comment3')
//output: /my%20page.aspx#comment2

encodeURIComponent('/my page.aspx#comment3')
//output: %2Fmy%20page.aspx%23comment2

encodeURI is for encoding non-URI special characters from a string so the above example of encodeURI would have been great for appending it like so 'http://www.britishdeveloper.co.uk' + encodeURI(url)

encodeURIComponent is for encoding a string to be fit for a querystring param where characters such as . and # shouldn't really be included as in my above example.

So know your Javascript encoding!

Follow britishdev on Twitter

40 comments:

  1. Excellent read, I just passed this onto a colleague who was doing a little research on that.

    how to write dissertation abstract

    ReplyDelete
  2. Very useful. Helped me solve exactly the same issue with "#" not encoding. Thanks

    ReplyDelete
  3. Browsers are tooooo much clever this days. They catch my '#' even while it encoded to %23.

    ReplyDelete
  4. Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me!



    Geometrydashonline.net
    Basketballlegendsgame.com

    ReplyDelete
  5. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium Lawn, Lawn 2019, Best lawn collection, Motifz, Best Lawn in Pakistan, Shipping worldwide. Stitching option available.

    ReplyDelete
  6. This is really unbelievable and extraordinary work. Everyone should read. Thanks for sharing and don't miss WORLD'S BEST

    BikeGame

    ReplyDelete
  7. Buy online wholesale printed custom candle boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!

    ReplyDelete
  8. Buy online wholesale custom cosmetic boxes and packaging at seashore packaging at the easy price Free shipping worldwide with 24/7 customer support.

    ReplyDelete
  9. Packaging town is a one-stop solution for the short-run Custom Packaging, Wholesale cream Boxes needs, and supplies at ease.

    ReplyDelete
  10. Blackbird packaging provide you with any size, shape design, game boxes wholesale sooner than any other packaging competitor in the market

    ReplyDelete
  11. i like your post watch online indian dramas full episodes hd..
    Qurbaan Hua
    Kundali Bhagya


    ReplyDelete
  12. coffee maker
    Shop for Electronic Goods at Al-Fatah, Pakistan's leading chain of departmental stores. Shop from a huge range of top brands Home Appliances, Smart-tech and Phones and Sound bars from Al-Fatah. Shop from huge range of electronics available at Al-Fatah. Avail Free Delivery on orders above 1000 PKR in selected cities.

    ReplyDelete
  13. buy lahore electronics from alfatah at best prices. get free shipping

    ReplyDelete
  14. buy online men zipper hoodie online from wild goat clothing at best prices. Get Free shipping

    ReplyDelete
  15. Blackbird packaging provide you with any size, shape design, custom bottle neckers wholesale sooner than any other packaging competitor in the market

    ReplyDelete
  16. Blackbird packaging provide you with any size, shape design, cookie packaging boxes sooner than any other packaging competitor in the market

    ReplyDelete
  17. buy hair supplements in pakistan in pakistan from medisense at best prices. Get free shipping

    ReplyDelete
  18. buy baby food online from medisense.pk at best prices delivered at your doorstep. get free shipping

    ReplyDelete
  19. buy dental care products at best prices from medisense delivered at your doorstep. Get free delivery

    ReplyDelete
  20. buy hair supplements in pakistan from Medisense.pk delivered at your doorstep at best prices. Get free shipping

    ReplyDelete
  21. buy tank tops for men online from dshred at easy prices delivered at your doorstep.get free shipping.

    ReplyDelete
  22. buy makeup in pakistan from alfatah at best prices delivered at your doorstep. get free delivery

    ReplyDelete
  23. buy skin care products in pakistan online from medisense at best prices deloivered at your doorstep. get free shipping

    ReplyDelete
  24. Buy buy multivitamins online pakistan at easy prices from medisense delivered at your doorstep. Get free shipping

    ReplyDelete
  25. Buy online medicine in lahore at affordable prices from medisense delivered at your doorstep. Get free shipping.

    ReplyDelete
  26. Buy men's care at easy prices from medisense delivered at your doorstep. Get free shipping.

    ReplyDelete
  27. buy sports tshirts online from dshred at easy prices delivered at your doorstep. get free shipping

    ReplyDelete
  28. Buy the latest pret lawn from Reign and enjoy the best price with free home delivery.

    ReplyDelete
  29. buy gym wear online in Pakistan from dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  30. buy electronics online in lahore from Al-Fatah at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  31. Buy quilt cover sets Online in Australia from Bulbul Home.

    ReplyDelete
  32. Choosing a bridal dress is a matter of extreme significance for the bride. Visit Highway Fashion And Choose the best.

    ReplyDelete
  33. Buy comfortable night dress for girls online in Pakistan from Enem Mall.

    ReplyDelete

  34. ECS is offering the best dresses for girls to make their collections fashionable. ECS fabrics are premium quality which make your outfit fancy.

    ReplyDelete
  35. i was quite pleased with myself remembering to URL Encode params that are going into a querystring to get around special characters but I had not thought of something.
    please visit link Tel-U

    ReplyDelete