473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

URLdecode ?

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
13 17557
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
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
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
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
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
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
>> 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Henz | last post by:
Hi again, can anyone tell me why it says that the object doesnt support this method? rs is a db query resultset. msg is a db field with urlencoded data in it. ------------ if not rs.eof then...
0
by: Yifan | last post by:
Hi I want to use UrlDecode() in C++. I have a Win32 Visual C++ (.NET) project #using <mscorlib.dll #using <System.dll #using <System.Web.dll using namespace System using namespace...
1
by: Joăo Santa Bárbara | last post by:
hi all is there any replace for the System.Web.HttpUtility.UrlDecode to windows forms ... thks JSB
5
by: Yifan | last post by:
Hi I want to use UrlDecode() in C++. I have a Visual C++ (.NET) Win32 project #using <mscorlib.dll #using <System.dll #using <System.Web.dll using namespace System using namespace...
4
by: John Hoge | last post by:
Hi, I need to UrlDecode a string in a simple console application, but I am having trouble accessing Server.UrlDecode, even though System.Web is included. The application pulls a text out of a...
6
by: John Grandy | last post by:
Does Server.URLDecode() need to be applied to Request.QueryString(<key name>) ?
10
by: Alex | last post by:
Hi, I UrlEncode a value that I put in my querystring. This value originally contains the character "č", which is UrlEncoded to "%E8". When I try to read the value from the querystring, I find...
1
by: fjm67 | last post by:
I see Google edited my email address. Lets try this. captonline at yahoo dot com Can you guys help me out please? I need to implement urldecode into a php script and don't know how. Here is...
20
by: gert | last post by:
This based on a example i found at http://www.cs.tut.fi/~jkorpela/ forms/cgic.html #include <fcgi_stdio.h> #include <stdlib.h> int urldecode(char *src, char *last, char *dest){ int code;...
3
by: gert | last post by:
Anybody can tell me what i need to import to make urlDecode() work in python2.5 please. import urllib urllib.urlDecode(post) #doesn't exist urllib.urldecode(post) #doesn't exist...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.