473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Encode And Decode entirety of Text or Html to '%xx' format

How would I modify this form
to encode *all* the characters
in the 'source' textarea to the
'%xx' format & place result
code into the 'output' textarea?
(cross browser compatable)

Any help is appreciated.

Regards.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Encode And Decode Entire Text or Html to '%xx' format
</title>
</head>
<body>
Text and Html "%xx" Converter<br>
<form name="form1" method="post" action="">
Original Text:<br>
<textarea name="source" cols="79" rows="8" wrap="VIRTUAL">Original Text or Html code
to have the *entirety* of
the characters converted
to '%xx' formated codes.
</textarea>
<br>
<br>
<br>
Output Text<br>
<textarea name="output" cols="79" rows="8" wrap="VIRTUAL"></textarea>
<br>
&nbsp;
<input type="button" name="Encode" value="Encode">
&nbsp;
<input type="button" name="Decode" value="Decode">
&nbsp;
<input name="reset" type="Reset" value="Reset">
</form>
</body>
</html>
Jul 23 '05 #1
4 4866
Newbie wrote:
How would I modify this form
to encode *all* the characters
in the 'source' textarea to the
'%xx' format & place result
code into the 'output' textarea?
(cross browser compatable)

Any help is appreciated.

Regards.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Encode And Decode Entire Text or Html to '%xx' format
</title>
</head>
<body>
Text and Html "%xx" Converter<br>
<form name="form1" method="post" action="">
Original Text:<br>
<textarea name="source" cols="79" rows="8" wrap="VIRTUAL">Original Text or Html code
to have the *entirety* of
the characters converted
to '%xx' formated codes.
</textarea>
<br>
<br>
<br>
Output Text<br>
<textarea name="output" cols="79" rows="8" wrap="VIRTUAL"></textarea>
<br>
&nbsp;
<input type="button" name="Encode" value="Encode">
&nbsp;
<input type="button" name="Decode" value="Decode">
&nbsp;
<input name="reset" type="Reset" value="Reset">
</form>
</body>
</html>


<script type="text/javascript">
function encode(f) {
var ta = f.elements['source'].value;
var hex;
var s = [];
for (var i = 0; i < ta.length; i++) {
hex = (ta.charCodeAt(i) % 256).toString(16);
s.push((hex.length < 2 ? '0' : '') + hex);
}
f.elements['output'].value = '%' + (s.join('%')).toUpperCase();
}
function decode(f) {
var ta = f.elements['source'].value.split(/%/);
var s = [];
for (var i = 0; i < ta.length; i++) {
s.push(String.fromCharCode(parseInt(ta[i], 16)));
}
f.elements['output'].value = s.join('');
}
</script>

<input type="button" name="Encode" value="Encode" onclick="encode(this.form);">
<input type="button" name="Decode" value="Decode" onclick="decode(this.form);">

I hope I get a good mark from your teacher.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


Jul 23 '05 #2
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message news:41***************@agricoreunited.com...
| Newbie wrote:
|
| > How would I modify this form
| > to encode *all* the characters
| > in the 'source' textarea to the
| > '%xx' format & place result
| > code into the 'output' textarea?
| > (cross browser compatable)
| >
| > Any help is appreciated.
| >
| > Regards.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Encode And Decode Entire Text or Html to '%xx' format
</title>
</head>
<body>
<script type="text/javascript">
<!--
function encode(f) {
var ta = f.elements['source'].value;
var hex;
var s = [];
for (var i = 0; i < ta.length; i++) {
hex = (ta.charCodeAt(i) % 256).toString(16);
s.push((hex.length < 2 ? '0' : '') + hex);
}
f.elements['output'].value = '%' + (s.join('%')).toUpperCase();
}
function decode(f) {
var ta = f.elements['source'].value.split(/%/);
var s = [];
for (var i = 0; i < ta.length; i++) {
s.push(String.fromCharCode(parseInt(ta[i], 16)));
}
f.elements['output'].value = s.join('');
}
//-->
</script>
Text and Html "%xx" Converter<br>
<form name="form1" method="post" action="">
Original Text:<br>
<textarea name="source" cols="79" rows="8" wrap="VIRTUAL">Original Text or Html code
to have the *entirety* of
the characters converted
to '%xx' formated codes.
</textarea>
<br>
<br>
<br>
Output Text<br>
<textarea name="output" cols="79" rows="8" wrap="VIRTUAL"></textarea>
<br>
&nbsp;
<input type="button" name="Encode" value="Encode" onclick="encode(this.form);">
&nbsp;
<input type="button" name="Decode" value="Decode" onclick="decode(this.form);">
&nbsp;
<input type="reset" name="Reset" value="Reset">
</form>
</body>
</html>

| I hope I get a good mark from your teacher.
|
| --
| Grant Wagner <gw*****@agricoreunited.com>
| comp.lang.javascript FAQ - http://jibbering.com/faq

I'm teaching myself & my teacher was really impressed :-)

Thanks.

PS:
Will this work with 3.xx and 4.xx old JS enabled browsers too?
Jul 23 '05 #3
Newbie wrote:
<script type="text/javascript">
<!--
<!-- not needed. Omit.
f.elements['output'].value = s.join('');
}
//-->
//--> not needed. Omit.
PS:
Will this work with 3.xx and 4.xx old JS enabled browsers too?


I would have said it'll work in almost any browser capable of JavaScript and forms, but I just discovered
that charCodeAt() wasn't implemented until JScript 5.5 <url:
http://msdn.microsoft.com/library/en...nformation.asp />. That has to be a
typo. I can't believe Microsoft didn't implement the String#charCodeAt() method until version 5.5. Anyway,
assuming that MS URL is right, the code I provided will probably only work in Internet Explorer 5.5 and
higher, although it should work in Netscape versions as low as 3 <url:
http://devedge.netscape.com/library/...g.html#1196647 />.

To make it work in versions of JScript older then 5.5, you'll have to add the following to your code:

if (!String.prototype.charCodeAt) {
String.prototype.charCodeAt = function (index) {
var charAt = this.charAt(index);
var i = 256;
while (i--) {
if (charAt == String.fromCharCode(i)) {
return i;
}
}
return 0;
}
}

Basically you're rolling your own String#charCodeAt() method that tests the character against each character
code from 255 to 0. I tested the method above for some simple cases, it seems to work correctly (until you
throw something like "String.fromCharCode(1027).charCodeAt2(0)" at it). Of course, if the browser is so old
that you have to use the above method definition, then it's likely it doesn't support unicode either. And
quite frankly, all the code I gave you assumes character codes in the range 0..255 (0x00..0xFF). Things would
be a bit more complicated if you allowed (0x0000..0xFFFF).

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #4
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message news:41***************@agricoreunited.com...
| Newbie wrote:
|
| > <script type="text/javascript">
| > <!--
|
| <!-- not needed. Omit.
|
| > f.elements['output'].value = s.join('');
| > }
| > //-->
|
| //--> not needed. Omit.
|
| > PS:
| > Will this work with 3.xx and 4.xx old JS enabled browsers too?
|
| I would have said it'll work in almost any browser capable of JavaScript and forms, but I just discovered
| that charCodeAt() wasn't implemented until JScript 5.5 <url:
| http://msdn.microsoft.com/library/en...nformation.asp />. That has to be a
| typo. I can't believe Microsoft didn't implement the String#charCodeAt() method until version 5.5. Anyway,
| assuming that MS URL is right, the code I provided will probably only work in Internet Explorer 5.5 and
| higher, although it should work in Netscape versions as low as 3 <url:
| http://devedge.netscape.com/library/...g.html#1196647 />.
|
| To make it work in versions of JScript older then 5.5, you'll have to add the following to your code:
|
| if (!String.prototype.charCodeAt) {
| String.prototype.charCodeAt = function (index) {
| var charAt = this.charAt(index);
| var i = 256;
| while (i--) {
| if (charAt == String.fromCharCode(i)) {
| return i;
| }
| }
| return 0;
| }
| }
|
| Basically you're rolling your own String#charCodeAt() method that tests the character against each character
| code from 255 to 0. I tested the method above for some simple cases, it seems to work correctly (until you
| throw something like "String.fromCharCode(1027).charCodeAt2(0)" at it). Of course, if the browser is so old
| that you have to use the above method definition, then it's likely it doesn't support unicode either. And
| quite frankly, all the code I gave you assumes character codes in the range 0..255 (0x00..0xFF). Things would
| be a bit more complicated if you allowed (0x0000..0xFFFF).
|
| --
| Grant Wagner <gw*****@agricoreunited.com>
| comp.lang.javascript FAQ - http://jibbering.com/faq

Thanks again for all of your help.
It is greatly appreciated that you
have taken the time & even went
to the trouble of checking MS &
Netscape, which I did not expect.

Kindest Regards.

PS: My teacher thanks you also :-)
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
3
by: fowlertrainer | last post by:
Hi ! I'm hungarian, we use special characters like: á - a' õ -o" etc. I want to encode this characters to in config file I see these characters as \nnn format.
3
by: Ricky | last post by:
Is there any way to detect if a field has been encoded before I decode a field.
5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
6
by: 7stud | last post by:
s1 = "hello" s2 = s1.encode("utf-8") s1 = "an accented 'e': \xc3\xa9" s2 = s1.encode("utf-8") The last line produces the error: --- Traceback (most recent call last):
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
1
by: anonymous | last post by:
1 Objective to write little programs to help me learn German. See code after numbered comments. //Thanks in advance for any direction or suggestions. tk 2 Want keyboard answer input, for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.