Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Url encode

0 views
Skip to first unread message

Serge

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
Hi,

I think there's a function to encode string to url.
Could you explain me how to use it ?

Best regards
Serge


Robert Lange

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
Hi Serge,

> I think there's a function to encode string to url.
> Could you explain me how to use it ?

location.href =
'http://www.website.net:8080/somePath/someFile.html?a=b&c=d#someTextAnchor'

--> location.protocol == 'http:'
loaction.host == 'website.net' (?)
location.hostname == 'www.website.net:8080'
location.port == '8080'
location.pathname == '/somePath/someFile.html'
location.search == '?a=b&c=d'
loaction.hash == '#someTextAnchor'

- Robert

Morten Wang

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
* NoS...@SpamWorld.com

| I think there's a function to encode string to url.
| Could you explain me how to use it ?

you mean how to encode/decode a URL-encoded string, e.g:

http://myserver.com/cgi-bin/somescript.cgi?filename=This%20is%20a%20space

?

escape() and unescape()


Morten!

--
Dijkstra's Law of Programming Inertia:
"If you don't know what your program is supposed to do,
you'd better not start writing it."

Pat Turner

unread,
Oct 18, 1999, 3:00:00 AM10/18/99
to
Morten Wang wrote:
>
> * NoS...@SpamWorld.com
> | I think there's a function to encode string to url.
> | Could you explain me how to use it ?
>
> you mean how to encode/decode a URL-encoded string, e.g:
>
> http://myserver.com/cgi-bin/somescript.cgi?filename=This%20is%20a%20space
>
> ?
>
> escape() and unescape()

This doesn't work for properly URL Encoded strings. For instance a space should be converted to a
plus (+), not the hex value of a space.

Does anybody else have a solution/function to hand?

regards
Pat

Morten Wang

unread,
Oct 18, 1999, 3:00:00 AM10/18/99
to
* Pat Turner

| This doesn't work for properly URL Encoded strings. For instance a
| space should be converted to a plus (+), not the hex value of a
| space.

I couldn't find anything in the RFCs that support the claim that spaces
should be converted to +, and not the hex value. I'd expect Jukka
Korpela to quickly correct me if I'm wrong here.

yes, I know the browsers use +, I'm just curious to find out exactly
_why_ it has to be converted to + and not %20. most Perl-scripts I've
seen use unpack to convert the hex value back anyway, and in JavaScript
it's a matter of saying unescape().

| Does anybody else have a solution/function to hand?

my escapeQuotes() function could probably be converted to doing
space-conversion and escaping at the same time.

function convertString(myString) {

if(myString.indexOf(" ") == -1) {
return(escape(myString));
} else {
var newString = "";
var cur_pos = myString.indexOf(" ");
var prev_pos = 0;
while (cur_pos != -1) {
if(cur_pos == 0) {
newString += "+";
} else {
newString += myString.substring(prev_pos, cur_pos) + "+";
}
prev_pos = ++cur_pos;
cur_pos = myString.indexOf(" ", cur_pos);
}
return(escape(newString + myString.substring(prev_pos, myString.length)));

Jukka....@hut.fi

unread,
Oct 19, 1999, 3:00:00 AM10/19/99
to
On 18 Oct 1999 16:07:16 +0200, Morten Wang <warn...@online.no> wrote:

>* Pat Turner
>| This doesn't work for properly URL Encoded strings. For instance a
>| space should be converted to a plus (+), not the hex value of a
>| space.
>
>I couldn't find anything in the RFCs that support the claim that spaces
>should be converted to +, and not the hex value.

There are two things involved:

1) The principle that all "unsafe" characters must be encoded in URLs.
This is defined by URL specifications, currently RFC 2396. In that
encoding, a space is to be written as %20. The encoding is very
simple: each "unsafe" character is encoded as %hh where hh is the code
position of the character in Ascii, with two hexadecimal digits. (The
definition of "unsafe" varies slightly between RFC 2396 and its
predecessor but the variation is not relevant here. Spaces are unsafe
in any case.)

2) The encoding of form data when application/x-www-form-urlencoded
(the default) is used, see
http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
This means that _first_ any space is replaced by the plus sign (+) and
_then_ characters are encoded as in URLs. (In fact, the data set
_will_ appear as part of a URL, so it really needs to be encoded.)

>yes, I know the browsers use +, I'm just curious to find out exactly
>_why_ it has to be converted to + and not %20.

When a browser performs a form submission, it applies 2). So if there
is a form with <input type="hidden" name="foo" value="me too">, then
its contribution to the form data set, as constructed by the browser,
must be foo=me+too.

But on the other hand, if you have a filename containing a space, say
"zip zap.html", among your Web pages, then you, as an author, need to
refer to it using a URL like
http://server/path/zip%20zap.html
Here only 1) applies; there is no form submission involved.

The escape and unescape functions in JavaScript relate to 1).
Thus, if you need to convert data to application/x-www-form-urlencoded
format, you must first change all spaces to plus signs, then apply the
escape function.
--
Yucca, http://www.hut.fi/u/jkorpela/
To join the Clueless Club, send a followup to this message
quoting everything up to and including this sig!

Pat Turner

unread,
Oct 19, 1999, 3:00:00 AM10/19/99
to
Many thanks to Morten and Jukka.

I'm a newbie to Javascript and the function provided should make my life a lot easier. I am used to
Java, where the java.net.URLEncoder class does it all for you :-)

p@

0 new messages