It’s Official: The 2019 Standard Deduction Is Getting Even Large
JavaScriptJqueryPHP

Escape javascript special characters compilation error using encodeURIComponent()

javascript_wallpaper

[dropcap]E[/dropcap]very programming language has it’s special characters – characters that mean something special such as identifying a variable, the end of a line or a break in some data. JavaScript is no different, so it provides a number of functions that encode and decode special characters.

The encodeURIComponent() function encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two “surrogate” characters).

Syntax
encodeURIComponent(str);

encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, – _ . ! ~ * ‘ ( )

To avoid unexpected requests to the server and escape Javascript special characters, you should call encodeURIComponent on any user-entered parameters that will be passed as part of a URI. For example, a user could type “Thyme &time=again” for a variable comment. Not using encodeURIComponent on this variable will give comment=Thyme%20&time=again. Note that the ampersand and the equal sign mark a new key and value pair. So instead of having a POST comment key equal to “Thyme &time=again”, you have two POST keys, one equal to “Thyme ” and another (time) equal to again.

The encodeURIComponent function is an extension of encodeURI, the difference being that it also escapes the following characters: , / ? : @ & = + $

About author

An enthusiast, creative web designer & developer working for Spryox.com
Related posts
LaravelPHP

How to rename users DB table in default Laravel Auth

APIGoogleGoogle URL ShortenerPHPYoutube

Steps to generate YouTube Data API and URL Shortener API

APIPHPSMS Gatewayssmshorizon

Simple sms integration of smshorizon api

PHPPHP Mailer

PHP Mailer failing with Warning: preg_match(): Compilation failed

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published. Required fields are marked *