By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,403 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,403 IT Pros & Developers. It's quick & easy.

URLdecode ?

P: n/a
Hi,

Is there an equivalent to URLdecode() in javascript ?

Details:
In Asp.Net I am using
HttpContext.Current.Response.Redirect("genericerro r.htm?ErrStr=" &
Err.Number & ": " & Err.Description)

In genericerror.htm I have:

<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;

if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write(value[1]);
}
</SCRIPT>

But I have problems with whitespace (and perhaps later other "funny" chars)
so how do I make the string "safe" or "back to normal" ?

tia
/jim
Jul 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
I beleive you want encodeURI and decodeURI (or maybe encodeURIComponent and
decodeURIComponent)

"Jim Andersen" <ji*@officeconsult.dk> wrote in message
news:bh**********@sunsite.dk...
Hi,

Is there an equivalent to URLdecode() in javascript ?

Details:
In Asp.Net I am using
HttpContext.Current.Response.Redirect("genericerro r.htm?ErrStr=" &
Err.Number & ": " & Err.Description)

In genericerror.htm I have:

<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;

if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write(value[1]);
}
</SCRIPT>

But I have problems with whitespace (and perhaps later other "funny" chars) so how do I make the string "safe" or "back to normal" ?

tia
/jim

Jul 20 '05 #2

P: n/a
kaeli wrote:
In article <bh**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...
But I have problems with whitespace (and perhaps later other "funny"
chars) so how do I make the string "safe" or "back to normal" ?


var x = unescape(url);


thx !

Along the same lines...

In .NET I use RegisterClientScriptBlock as a kind of msgbox.
msgStr="Hello"
pageref.RegisterClientScriptBlock("aaa", "<script language=JavaScript>
alert('" & msgstr & "')</SCRIPT>")

works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
content of msgStr is of course unknown to me at runtime.

Whats the syntax for escaping msgstr ?

tia
/jim

Jul 20 '05 #3

P: n/a
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...
In .NET I use RegisterClientScriptBlock as a kind of msgbox.
msgStr="Hello"
pageref.RegisterClientScriptBlock("aaa", "<script language=JavaScript>
alert('" & msgstr & "')</SCRIPT>")

works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
content of msgStr is of course unknown to me at runtime.

Whats the syntax for escaping msgstr ?


Technically? A backslash before any quotes. That's the hard way, though.
Make the single quotes into double quotes and escape them so they don't
mess up your VB string. Then you won't have to worry about single
quotes, but double quotes will still mess you up.
In VB, IIRC, to escape a double quote literal you preface it with
another double quote. In javascript, you use a backslash.

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #4

P: n/a
kaeli wrote:
Whats the syntax for escaping msgstr ?
In javascript, you use a backslash.

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak that
javascript out of working ?

/jim
Jul 20 '05 #5

P: n/a
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak that
javascript out of working ?


Just having embedded double quotes, AFAIK.

I don't think any other normal characters would be a problem. I assume
since you have control of the string, no really odd characters (like
non-printing chars or foreign chars) would be entered.

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #6

P: n/a
kaeli wrote:
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need
to worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak
that javascript out of working ?


Just having embedded double quotes, AFAIK.

I don't think any other normal characters would be a problem. I assume
since you have control of the string, no really odd characters (like
non-printing chars or foreign chars) would be entered.


I actually do
msgstr=err.Number & ": " & err.Description
and once .Description was
no such object 'zzzTblTst'
I am gonna experiment with server.getlasterror etc, and that could (AFAIK)
include both [ and & and { etc.

/jim
Jul 20 '05 #7

P: n/a
>> But I have problems with whitespace (and perhaps later other "funny"
chars)
so how do I make the string "safe" or "back to normal" ?
var x = unescape(url);


hmmm, nope.

Have searched hi&lo on google and it seems it's a known issue. .Net and Java
doesn't use the same algorithm for encoding-decoding. So I don't get my
original string.

/jim
Jul 20 '05 #8

P: n/a
On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
Have searched hi&lo on google and it seems it's a known issue. .Net and Java
doesn't use the same algorithm for encoding-decoding. So I don't get my
original string.


decodeURIComponent

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9

P: n/a
Jim Ley wrote:
On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
Have searched hi&lo on google and it seems it's a known issue. .Net
and Java doesn't use the same algorithm for encoding-decoding. So I
don't get my original string.


decodeURIComponent

Jim.


So why isn't this working:

foo.aspx:
Response.Redirect("tst.htm?msg=" & HttpUtility.UrlEncode("6: division by
zero"))

tst.htm
<body>
Msg=
<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;
if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write( value[1]);
}
</SCRIPT>
</body>

Result: I get Msg= 6%3a+division+by+zero

I change tst.htm:
var=FunctionToGetQuerystring;
document.write(decodeURIComponent(var))

Result: I get Msg= 6:+division+by+zero

If I change foo.aspx:
Response.Redirect("tst.aspx?msg=" & HttpUtility.UrlEncode("6: division by
zero"))

and have tst.aspx do:
Response.Write("msg=" & HttpUtility.UrlDecode(Request.QueryString("msg")))
I finally get the desired result
msg=6: division by zero

regards
Jim Andersen
Jul 20 '05 #10

P: n/a
On Fri, 5 Sep 2003 12:10:18 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
foo.aspx:
Response.Redirect("tst.htm?msg=" & HttpUtility.UrlEncode("6: division by
zero"))
Result: I get Msg= 6:+division+by+zero


It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,
your problem is with that, not with javascript.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #11

P: n/a
ji*@jibbering.com (Jim Ley) writes:
It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,


Well, for some definition of correct, it is. It is the same way form
data are encoded for the GET transfer method.

The solution is to change all plusses to spaces before decoding with
unescape:

var decoded = unescape(encoded.replace(/\+/g," "));

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12

P: n/a
On 05 Sep 2003 12:48:38 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
ji*@jibbering.com (Jim Ley) writes:
It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,


Well, for some definition of correct, it is. It is the same way form
data are encoded for the GET transfer method.


True, but it is not URIEncoding which the method name suggests.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #13

P: n/a
Lasse Reichstein Nielsen wrote:
The solution is to change all plusses to spaces before decoding with
unescape:

var decoded = unescape(encoded.replace(/\+/g," "));


Thx Lasse for the helpful answer (as opposed to Jim Ley's fantasies).

/jim
Jul 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.