473,570 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4891
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(1 6);
s.push((hex.len gth < 2 ? '0' : '') + hex);
}
f.elements['output'].value = '%' + (s.join('%')).t oUpperCase();
}
function decode(f) {
var ta = f.elements['source'].value.split(/%/);
var s = [];
for (var i = 0; i < ta.length; i++) {
s.push(String.f romCharCode(par seInt(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*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq


Jul 23 '05 #2
"Grant Wagner" <gw*****@agrico reunited.com> wrote in message news:41******** *******@agricor eunited.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(1 6);
s.push((hex.len gth < 2 ? '0' : '') + hex);
}
f.elements['output'].value = '%' + (s.join('%')).t oUpperCase();
}
function decode(f) {
var ta = f.elements['source'].value.split(/%/);
var s = [];
for (var i = 0; i < ta.length; i++) {
s.push(String.f romCharCode(par seInt(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*****@agrico reunited.com>
| comp.lang.javas cript 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#charCode At() 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.protot ype.charCodeAt) {
String.prototyp e.charCodeAt = function (index) {
var charAt = this.charAt(ind ex);
var i = 256;
while (i--) {
if (charAt == String.fromChar Code(i)) {
return i;
}
}
return 0;
}
}

Basically you're rolling your own String#charCode At() 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.fromCha rCode(1027).cha rCodeAt2(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*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #4
"Grant Wagner" <gw*****@agrico reunited.com> wrote in message news:41******** *******@agricor eunited.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#charCode At() 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.protot ype.charCodeAt) {
| String.prototyp e.charCodeAt = function (index) {
| var charAt = this.charAt(ind ex);
| var i = 256;
| while (i--) {
| if (charAt == String.fromChar Code(i)) {
| return i;
| }
| }
| return 0;
| }
| }
|
| Basically you're rolling your own String#charCode At() 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.fromCha rCode(1027).cha rCodeAt2(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*****@agrico reunited.com>
| comp.lang.javas cript 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
8107
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. -------------------- ---------- ---------- ------- ------- 21/03/2005-04:06:03 XX,XX XX,XX 171 3,42 21/03/2005-12:23:53 XX,XX ...
3
2302
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
2564
by: Ricky | last post by:
Is there any way to detect if a field has been encoded before I decode a field.
5
4085
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 encode() function behaves differently if a codepage has been set. For example: <script> document.write(escape('Ôèëìè')); (note: that should be five...
4
7559
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 the HTML text are throwing a SQL error. Html.encode doesn't seem to do anything with these, eh? Secondly, does HTMLencode also encode already...
7
8999
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 can encode something using: C#: System.Security.Cryptography (to encode) and
6
2325
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
8225
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 am trying to fix things with <string>.encode he Company�s ticker Trying for an encode:
1
5764
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 example: answer_str = raw_input(' Enter answer ') Herr Üü
0
7637
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7950
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7702
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6332
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2133
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.