URLdecode ?

J

Jim Andersen

Hi,

Is there an equivalent to URLdecode() in javascript ?

Details:
In Asp.Net I am using
HttpContext.Current.Response.Redirect("genericerror.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
 
A

Andy Fish

I beleive you want encodeURI and decodeURI (or maybe encodeURIComponent and
decodeURIComponent)
 
J

Jim Andersen

kaeli said:
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
 
K

kaeli

[email protected] enlightened said:
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
-------------------------------------------------
 
J

Jim Andersen

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
 
K

kaeli

[email protected] enlightened said:
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
-------------------------------------------------
 
J

Jim Andersen

kaeli said:
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
 
J

Jim Andersen

But I have problems with whitespace (and perhaps later other "funny"
chars)
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
 
J

Jim Ley

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.
 
J

Jim Andersen

Jim said:
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
 
J

Jim Ley

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.
 
L

Lasse Reichstein Nielsen

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
 
J

Jim Ley

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.
 
J

Jim Andersen

Lasse said:
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
 

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
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top