views:

34

answers:

2

I'm trying to do it in VBScript/JScript, to avoid re-encoding.
Should I judge if there is "%" ? Does "%" have other uses in URL?
Thanks.

Edit: Oh, the original encoding function may not be encodeURI.
I'm trying to collect URLs from the browser, and store them after encoding with encodeURI.
But if the URL is already encoded, another encoding will make it wrong.

A: 

I might try decoding it and comparing the result to the original URL. If it changed or got shorter in length your original URL was probably already encoded.

Cory Larson
Thank you. So a URL which hasn't been encoded can't be decoded. Is this always true?
phoenies
I'm not 100% on that -- but it might be an okay starting point. You might run into some funny URLs that won't fit; maybe some that are supposed to have the encoded characters in them.
Cory Larson
OK. This is what I got: decodeURI throws unknown error 800A13A1. Google says it's because the string to be decoded contains illegal characters.
phoenies
A: 

iterate over the chars in the url and test for characters that aren't allowed in an url.

if there are any encode it. if there aren't any illegal characters, it doesn't matter

Nikolaus Gradwohl
Sounds great. Erm...what characters are illegal then? I know even encodeURI and URLencode have different opinions.
phoenies
as defined in RFC1738 safe characters are 0-9a-zA-Z and $-_.+!*'(), everything else is unsafe
Nikolaus Gradwohl
Cory Larson
It needs much care, and a new RegExp object. I don't tend to use it. Thank you anwyay.
phoenies
you don't need to use regex on this. just make a string containing all the valid chars, iterate over the chars in your url and test if every char in the url is contained in your safe-char-string if you find one that isn't your url needs to be urlencoded
Nikolaus Gradwohl