Escape Ampersand in URL in JavaScript

In this post, we will see how to escape ampersand in JavaScript.

To escape Ampersand in JavaScript, use encodeURIComponent() in JavaScript.

Output

As you can see that when we called encodeURIComponent, it encoded & to %26.

You can use % to escape characters that aren’t allowed in URLs. Here & is 26 in Hexadecimal and that’s the reason encodeURIComponent() converted & to %26.

Let’s see with the help of simple example:

Let’s say company_name is A&B and url is:

http://example.com?company_name=A&B

if don’t escape &, then you won’t get expected result.

To escape & character, you can use encodeURIComponent() as below

Output

As you can see, we have successfully escape ampersand in URL.

Generic solution to escape special character in JavaScript

Great thing about our solution is that it will work with any special character and you can same solution.

Output

As you can see that we were able to successfully escape all special characters using encodeURIComponent().

Was this post helpful?

Related Posts

  • 01 February

    Write Binary Files in PowerShell

    Table of ContentsUsing Add-Content CmdletUsing the [IO.File]::WriteAllBytes() MethodUsing FromBase64CharArray() with Add-Content Using Add-Content Cmdlet Use the Add-Content cmdlet to write binary files in PowerShell. [crayon-63da9be1ce0c5004186196/] This script created a new file named test.bin in the current working directory and wrote the binary data to the file. The script will overwrite the existing file with the […]

  • 01 February

    What Does Percent(%) Mean in PowerShell

    Table of ContentsUsing Percent (%) in Different ContextsUse Percent(%) as an alias of the ForEach-Object cmdlet in PowerShellUse percent (%) as the Modulus Operator in PowerShellUse percent(%) as an assignment operator (%=) in PowerShell Using Percent (%) in Different Contexts Typically the % character is for mod functionality. But in PowerShell, it’s an alias for […]

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63da9be1ce632618873932/] [crayon-63da9be1ce636480874526/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

  • Get String between two characters in JavaScript
    27 January

    Get String Between Two Characters in JavaScript

    Table of ContentsUsing substring() MethodUsing slice() MethodUsing split() MethodUsing substr() Method 💡TL;DR Use the substring() method to get String between two characters in JavaScript. [crayon-63da9be1ce9a2943124190/] [crayon-63da9be1ce9a6058219596/] Here, we got String between , and ! in above example. Using substring() Method Use the substring() method to extract a substring that is between two specific characters from […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.