/

HTML / CSS / JavaScript Tutorial

JavaScript Global method: escape()

[this page | pdf | back links]

The JavaScript Global escape() method encodes a string representing a URI by replacing certain characters by one, two, three or (rarely) four escape sequences representing the UTF-8 encoding of the character. It is depreciated, and encodeURI or encodeURIComponent should be used instead.

 

It has the following syntax with the following parameters:

 

escape(URI)

 

Parameter

Required / Optional

Description

URI

Required

String representing URI to be encoded

 

More details on URI encoding is given here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse :collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="ExampleE"></code></td>
<td><code id="ResultE"></code></td>
</tr>
<tr>
<td><code id="ExampleEC"></code></td>
<td><code id="ResultEC"></code></td>
</tr>
<tr>
<tr>
<td><code id="ExampleD"></code></td>
<td><code id="ResultD"></code></td>
</tr>
<tr>
<td><code id="ExampleDC"></code></td>
<td><code id="ResultDC"></code></td>
</tr>
<tr>
<td><code id="ExampleEscape"></code></td>
<td><code id="ResultEscape"></code></td>
</tr>
<tr>
<td><code id="ExampleUnescape"></code></td>
<td><code id="ResultUnescape"></code></td>
</tr>
</table>

<script>
document.getElementById("ExampleE").innerHTML =
  'var x = encodeURI("a1!%?");';
document.getElementById("ResultE").innerHTML =
  encodeURI("a1!%?");
document.getElementById("ExampleEC").innerHTML =
  'var x = encodeURIComponent("a1!%?");';
document.getElementById("ResultEC").innerHTML =
  encodeURIComponent("a1!%?");
document.getElementById("ExampleD").innerHTML =
  'var x = decodeURI("a1!%25?");';
document.getElementById("ResultD").innerHTML =
  decodeURI("a1!%25?");
document.getElementById("ExampleDC").innerHTML =
  'var x = decodeURIComponent("a1!%25%3F");';
document.getElementById("ResultDC").innerHTML =
  decodeURIComponent("a1!%25%3F");
document.getElementById("ExampleEscape").innerHTML =
  '(depreciated)<br>var x = encape("a1!%?");';
document.getElementById("ResultEscape").innerHTML =
  escape("a1!%?");
document.getElementById("ExampleUnescape").innerHTML =
  '(depreciated)<br>var x = unescape("a1%21%25%3F");';
document.getElementById("ResultUnescape").innerHTML =
  unescape("a1!%25?");
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodGlobalEscape() {
  return (escape("!%?") == "%21%25%3F");
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile