Any Other Business JS Home << JS Advanced << Any Other Business
This lesson wraps up our journey into the world of JavaScript by looking at the globals and statements not covered in previous lessons.
Encoding & Decoding URIs
We can encode a uri using the encodeURI global function. This function will encode special characters within the URL, apart from those parts used by component
parts of the uri, such as user parameters.
We can also decode any previously encoded uri using the decodeURI global function. This function will decode special characters within the URL,
apart from those parts used by component parts of the uri, such as user parameters.
Below is an example of using the encodeURI global function to encode special characters. After this we decode the uri we encoded using the
decodeURI global function. We should end up with the same uri we had before we encoded and then decoded it.
// Encode and then decode a uri
var uri="a spurious name.com?firstname=barney&surname=magrew";
alert(encodeURI(uri));
alert(decodeURI(uri));
Encoding & Decoding Component URIs
We can encode a uri using the encodeURIComponent global function. This function will encode special characters within the URL and component
parts of the uri, such as user parameters.
We can also decode any previously encoded uri using the decodeURIComponent global function. This function will decode special characters within the URL
as well as component parts of the uri, such as user parameters.
Below is an example of using the encodeURIComponent global function to encode special characters. After this we decode the uri we encoded using the
decodeURIComponent global function. We should end up with the same uri we had before we encoded and then decoded it.
// Encode and then decode a uri
var uri="a spurious name.com?firstname=barney&surname=magrew";
alert(encodeURIComponent(uri));
alert(decodeURIComponent(uri));
The debugger statement
The debugger statement starts up any available debugging functionality, or does nothing if no debugging functionality available.
The code below invokes a debugging tool if functionality available. For instance if you have Firebug installed on Firefox this will be launched when code is run.
function goDebugger()
{
debugger;
alert('In debugger');
}
goDebugger();
Lesson 9 Complete
In this lesson we looked at encoding and decoding URIs and the debugger statement.
Reference
JavaScript Reference - decodeURI global function
JavaScript Reference - decodeURIComponent global function
JavaScript Reference - encodeURI global function
JavaScript Reference - encodeURIComponent global function
JavaScript Reference - debugger statement
