How do I turn every chactor into %xx style?

O

ok

I tried escape, but it is only good for the special ones like : / ' space
etc. I want to turn every charactor into %xx then document.write them.

I wonder if i can double % them so I can turn @ into %40, then %%xx%yy xx is
for 4 and yy is for 0.

Thanks
 
E

Evertjan.

ok wrote on 24 sep 2004 in comp.lang.javascript:
I tried escape, but it is only good for the special ones like : / '
space etc. I want to turn every charactor into %xx then document.write
them.

<script type='text/javascript'>

function toPerc(t){
var tt = '';
for (var i=0;i<t.length;i++){
tt += "%"+t.charCodeAt(i);
};
return tt;
}

document.write(toPerc('Blah blah'));

I wonder if i can double % them so I can turn @ into %40, then %%xx%yy
xx is for 4 and yy is for 0.

You could, but html cannot read that.

did you try?
 
F

Fred Oz

ok said:
I tried escape, but it is only good for the special ones like : / ' space
etc. I want to turn every charactor into %xx then document.write them.

I wonder if i can double % them so I can turn @ into %40, then %%xx%yy xx is
for 4 and yy is for 0.

Not really sure what you are trying to do here - encode is dpreciated
in favour of encodeURI. If you are after a way of obfuscating your
code, Google "javascript encode"

Have a look at the link below, it includes a script to convert all
characters into their JavaScript encoding. Note that the encoding for
normal numbers and alphas is themselves,
i.e. the encoding for "a" is "a".

http://lab.artlung.com/scripting/urlencode/

Checkout encodeURI too.

Cheers, Fred.
 
J

John Bokma

ok wrote on 24 sep 2004 in comp.lang.javascript:

<script type='text/javascript'>

function toPerc(t){
var tt = '';
for (var i=0;i<t.length;i++){
tt += "%"+t.charCodeAt(i);

should be in hex, doubt that it is... Also, it should be *two* hex
digits afaik, ie. %a%b%c is wrong.
};
return tt;
}

document.write(toPerc('Blah blah'));



You could, but html cannot read that.

Has nothing to do with HTML, but what your browser can do. And it can't.
 
E

Evertjan.

John Bokma wrote on 24 sep 2004 in comp.lang.javascript:
should be in hex, doubt that it is... Also, it should be *two* hex
digits afaik, ie. %a%b%c is wrong.


You are right, if the html standard expects hex. And it does. ;-)

=============================

<script type='text/javascript'>

var r = '0123456789ABCDEF'

function toPerc(t){
var tt = '';
for (var i=0;i<t.length;i++){
x = t.charCodeAt(i)
tt += "%"+r.substr(x/16,1)+r.substr(x%16,1);
};
return tt;
}

document.write(toPerc('123Blah blah'));

</script>
 
J

John Bokma

John Bokma wrote on 24 sep 2004 in comp.lang.javascript:


You are right, if the html standard expects hex. And it does. ;-)

It has nothing to do with HTML, but with URI encoding.
tt += "%"+r.substr(x/16,1)+r.substr(x%16,1);

:-D.
 
G

Grant Wagner

Evertjan. said:
John Bokma wrote on 24 sep 2004 in comp.lang.javascript:


You are right, if the html standard expects hex. And it does. ;-)

=============================

<script type='text/javascript'>

var r = '0123456789ABCDEF'

function toPerc(t){
var tt = '';
for (var i=0;i<t.length;i++){
x = t.charCodeAt(i)
tt += "%"+r.substr(x/16,1)+r.substr(x%16,1);
};
return tt;
}

document.write(toPerc('123Blah blah'));

</script>

function toPerc(t){
var tt = '', hex;
for (var i = 0; i < t.length; ++i) {
tt += '%' + (hex = ('0' +
t.charCodeAt(i).toString(16).toUpperCase())).substring(hex.length - 2);
}
return tt;
}
document.write(toPerc('123\t\0Blah blah'));

Avoids a "magic" string containing the available hex digits.

I wanted to use:

tt += '%' + ('0' +
t.charCodeAt(i).toString(16).toUpperCase()).substr(-2);

which would have avoided the awkward assignment to "hex", but IE doesn't
respect the substr() contract as far as negative first parameters are
concerned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top