DerickBailey.com

Trade Secrets Of A Developer / Entrepreneur

  •  About
  •  Twitter
  •  G+
  •  RSS
  • Blog
  • Courses
  • Products
  • Newsletter
  • Publications
  • Podcasts
  • Work With Me!

URI/URL Encoding Strings In JavaScript/Node.js

May 2, 2016 By Derick

I recently found myself needing to URL encode a string, in Node – to convert a space into %20 in this case, but also to handle other scenarios.

My initial thought was “oh, great – I’ll need to find a library that doesn’t completely suck”.

Turns out I didn’t need to find anything. JavaScript has a built-in function to handle this: encodeURI

Pass any string to it, and it will produce the correctly encoded string as output:

There is a decodeURI method as well, to take an encoded string and produce a decoded version:

These methods have been in JavaScript since EMCAScript 3, I just never knew about them until a few days ago.

The next time you need to encode or decode a URL or other URI string, then, you can do so with JavaScript’s built-in functions.

Tweet

 


 

Related Post

Docker for JavaScript Developers: On-Site Training
3 Features of ES7 (and Beyond) That You Should Be ...
How a 650MB Node.js Image for Docker Uses Less Spa...
3 Rules For When A New JavaScript Feature Is Ready...
Never Use The :latest Image From Docker Hub

Filed Under: Decoding, Encoding, ES3, ES5, ES6, Functions, JavaScript, Tips And Tricks, URL

About Derick

Derick Bailey is a developer, entrepreneur, author, speaker and technology leader in central Texas (north of Austin). He's been a professional developer since the late 90’s, and has been writing code since the late 80’s. Derick has built software for organizations of all shapes and sizes, including contributions to Microsoft's MDSN library, running several very highly regarded open source projects, creating software solutions for large financial organizations, healthcare orgnaizations, world-class airlines, the U.S. government, and more. These days, Derick spends most of his time working on content for his own entrepreneurial efforts at WatchMeCode.net, playing video games when he gets a chance, and writing code for for his few remaining clients. You can reach Derick at DerickBailey.com or on twitter, @derickbailey.

Derick Bailey Around The Web

  • Twitter: @derickbailey
  • Google+: DerickBailey
  • Screencasts: WatchMeCode.net
  • eBook: Building Backbone Plugins

Copyright © 2016 Muted Solutions, LLC. All Rights Reserved · Log in