Solved

JavaScript: URL Encode All Non-whitelisted Characters Using _ instead of %

Posted on 2013-11-06
1
239 Views
Last Modified: 2013-11-07
I want JavaScript functions that will work just like escape() and unescape() that use an underscore instead of a percent and encode all non-whitelisted charachters.

My function below does not work, as the example demonstrates:
http://jsfiddle.net/XVqeR/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>

<script type="text/javascript">
//<![CDATA[
// Thanks, leakim971
var x = 'A _test_ "1-2-3" 50%';
alert('The fist and last line should be the same:\n\n'+x+'\n'+escapeNonWhitelistedU(x)+'\n'+unescapeU(escapeNonWhitelistedU(x)));


function escapeNonWhitelistedU(str) {
    var escapechar = "%";
    var whitelist = "bcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    return str.replace(/(.)/g,function($1) { return whitelist.indexOf($1)<0?escapechar+$1.charCodeAt(0).toString(16):$1; });
}

function unescapeU(str) {
 return unescape(str.replace(/_/g,"%"));
}

//]]>
</script>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
your escape character CAN'T be in your whitelist else how to differentiate both?

the same way escape convert % to %25

you should use a char not in your whitelist so you know when you found it it's the escape char
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now