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

43 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. Buy Premium lawn Online for Women's at best prices available at Motifz. ✓ Latest Fashion, ✓ Trendy Designs ➤ Shop Now!

    ReplyDelete
  7. Packers and Movers Bangalore as a Services providing company can make all the difference to your ###Home Relocation experience. Bangalore based Company which offers versatile solutions, Right team that easily reduce the stress associated with a ???Household Shifting, ***Vehicle Transportation. we help things run smoothly and reduce breakages and offer you seamless, Affordable, Reliable Shifting Services, Compare Shifting Charges
    Packers And Movers Bangalore

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

    BikeGame

    ReplyDelete
  9. Hire Best Packers And Movers Mumbai for hassle-free Household Shifting, ***Office Relocation, ###Car Transporation, Loading Unloading, packing Unpacking at affordable Price Quotation. Top Rated, Safe and Secure Service Providers who can help you with 24x7 and make sure a Untroubled Relocation Services at Cheapest/Lowest Rate @ Packers And Movers Mumbai

    ReplyDelete
  10. Packers And Movers Bangalore Local Household Shifting Service, Get Free Best Price Quotes Local Packers and Movers in Bangalore List, Compare Charges, Save Money And Time at
    Local Packers And Movers Bangalore

    ReplyDelete
  11. Packers and Movers Pune Provide High Quality ***Household Shifting, Home/Office Relocation, Insurance, Packing, Loading, ###Car Transportation Service Pune and High experiences, Top Rated, Safe and Reliable, Best and Secure Packers and Movers Pune Team List. Get ✔✔✔Affordable Rate Charts and Compare Quotation and Save Money and Time @
    Packers And Movers Pune

    ReplyDelete
  12. Packers and Movers Hyderabad Give Certified and Verified Service Providers, Cheap and Best ###Office Relocation Charges, ***Home Shifting, ✔✔✔Goods Insurance worth Rs. 10,000, Assurance for Local and Domestic House Shifting. Safe and Reliable Household Shifting Services in Hyderabad with Reasonable Packers and Movers Price Quotation @ Packers And Movers Hyderabad

    ReplyDelete
  13. Packers and Movers Gurgaon Provide Reliable, Safe and Certified Service Provider list, Get Free ***Best Price Quotaition and Compare Charges. ???Hassle free Household Shifting Services, High Quality packing Material, Office Relocation, Car Transportaion, ###Local and Domestic Shifting Service @
    Packers And Movers Gurgaon

    ReplyDelete
  14. Get Shifting/Relocation Quotation from ###Packers and Movers Delhi. Packers and Movers Delhi 100% Affordable and Reliable ***Household Shifting Services. Compare Transportation Charges and Save Time, Verified and Trusted Packers and Movers in Delhi, Cheap and Safe Local, Domestic House Shifting @ Packers And Movers Delhi

    ReplyDelete
  15. Nice post!..good information,it is really helpful..it really impressed me alot and i just loved it.Thanks for posting such an informative content:
    Local Packers And Movers Bangalore

    ReplyDelete
  16. Packers And Movers Hyderabad Local Household Shifting Service, Get Free Best Price Quotes Local Packers and Movers in Hyderabad List , Compare Charges, Save Money And Time.@
    @ Packers And Movers Hyderabad

    ReplyDelete
  17. Wholesale printed custom perfume boxes with the highest quality and affordable prices. Free worldwide Delivery and free Custom Perfume Boxes design.

    ReplyDelete
  18. 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
  19. Packers and Movers Chennai list, Cheap Best Packers Movers Chennai Charges, Local, Affordable Household Shifting Chennai @ Packers And Movers Chennai

    ReplyDelete
  20. We Provide Local Packers And Movers Pune List for Get Best Price Quotes, Compare Charges, Save Money And Time, Top Household Shifting Services @ Packers And Movers Pune

    ReplyDelete
  21. an awesome blog thanks a lot for giving me this great opportunity to write on this.please visit our website I assure you that it will be very useful for you @ Packers and Movers Kolkata

    ReplyDelete
  22. Packers and Movers Chennai list, Cheap Best Packers Movers Chennai Charges, Local, Affordable Household Shifting Chennai @ Packers And Movers Chennai

    ReplyDelete
  23. Lovely Website, Maintain the fantastic work. Thank you so much!
    Local Packers and Movers Mumbai list, Cheap Packers Movers Mumbai Charges, Affordable, Best Household Shifting Mumbai @ Packers and Movers mumbai

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

    ReplyDelete
  25. Packers and Movers Ahmedabad list, Cheap Packers Movers Ahmedabad Charges, Local, Affordable Household Shifting Ahmedabad @ Packers and Movers Ahmedabad

    ReplyDelete
  26. Get Packers and Movers Jaipur List of Top Reliable, 100% Affordable, Verified and Secured Service Provider. Get Free ###Packers and Movers Jaipur Price Quotation instantly and Save Cost and Time. Packers and Movers Jaipur ✔✔✔Reviews and Compare Charges for household Shifting, Home/Office Relocation, ***Car Transportation, Pet Relocation, Bike SHifting @ Packers And Movers Jaipur

    ReplyDelete
  27. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ? ? ? Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @ Packers And Movers Chennai

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

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

    ReplyDelete
  30. mylar label bags in stock now. Ready made Cali pack designs. Fast Shipping at Your Supplies. Custom designs available

    ReplyDelete
  31. An excellent information provided thanks for all the information i must say great efforts made by you. thanks a lot for all the information you provided
    Packers And Movers Jaipur

    ReplyDelete