Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2010, 07:33 AM   PM User | #1
starmandell
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
starmandell is an unknown quantity at this point
decode input into url

hi, i was wondering if anyone could help me with decoding an input box value and passing that into the url. its been super frurstrating trying to do this. thank you in advance!

basically i wrote a function that decodes a url. for example if i enter t05:34:23-79-79.003z.444z, i want it to stay like that when it goes into the url. if you use internet explorer, entering that input becomes... t05%3A34%3A23-79-79.003z.444z. on google chrome, it stays as t05:34:23-79-79.003z.444z but i need this to work on internet explorer.

[<html>
<head>
<script type="text/javascript">
function decode() {
var obj = document.getElementById("workorder");
alert(obj); //is it normal that this alert just returns 'object'? am i calling the form wrong somehow?
var encoded = obj.value;
obj.value = decodeURIComponent(encoded.replace(/\+/g, " "));

}
</script>
</head>
<body>
<div id="workorder" style="display: block">
<form name = "workorder" action="http://localhost/Lights/WorkOrder.jsp" onsubmit="return validate_form(this)" method="get"><b>WorkOrder:</b>
<br> Id Required: <input type="hidden" name="method"
value="getWorkOrders">
<input type="text" name="lightId" size="30">
<input type="submit" onclick="decode()" value="Go"> <br>
</form>
</div>
</body>
</html>]

thank you advance!
starmandell is offline   Reply With Quote
Old 08-08-2010, 12:19 PM   PM User | #2
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi


Quote:
alert(obj); //is it normal that this alert just returns 'object'? am i calling the form wrong somehow?
Yes and Yes.


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
function decodeIt() {

	var uri=document.workorder.lightId.value;
		alert(uri);	
	var encodeuri = encodeURIComponent(uri);
		alert(encodeuri);
	var decodeuri = decodeURIComponent(encodeuri);
		alert(decodeuri);
		
	location.href= decodeuri;
}
</script>
</head>
<body>
<div id="workorder" style="display: block">
<form name = "workorder" action="http://localhost/Lights/WorkOrder.jsp" onsubmit="return validate_form(this)" method="get"><b>WorkOrder:</b>
<br> Id Required: <input type="hidden" name="method"
value="getWorkOrders"> 
<input type="text" name="lightId" size="30">
<input type="submit" onclick="decodeIt()" value="Go"> <br>
</form>
</div>
</body>
</html>
ps the function is NOT intended to be a complete answer to your problem ---- althouh it might help in some way
low tech is offline   Reply With Quote
Old 08-09-2010, 01:27 AM   PM User | #3
starmandell
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
starmandell is an unknown quantity at this point
oh thanks for telling me how to call it correctly = )

is there a way to put the decoded into the url?
starmandell is offline   Reply With Quote
Old 08-09-2010, 02:14 AM   PM User | #4
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

Have you tried the script?

The location.href part puts the decoded into the url. Type something into the input -- it will be passed to the url.

The alerts are there to help you work out your problem.

I'm not a pro coder and as I said this is NOT intended to be a COMPLETE answer to your problem.

good luck.
low tech is offline   Reply With Quote
Old 08-09-2010, 02:19 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You *WANT* the ENCODED in the URL. If the query string is not encoded, then the servers may not handle it correctly!

What you really need is to be able to *decode* the URL.

And your JSP page *SHOULD* be doing that automatically, for you.

The standard JSP methods to get the value of a single form field *will* decode it properly.

If there is a problem here, it is surely in your JSP code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 08-09-2010, 02:23 AM   PM User | #6
starmandell
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
starmandell is an unknown quantity at this point
i tried running the script, but the encoded version still gets passed into the url. passing in t05:34:23-79-79.003z.444z into the input box still results in http://localhost/Lights/WorkOrder.js...9-79.003z.444z in internet explorer.
starmandell is offline   Reply With Quote
Old 08-09-2010, 02:26 AM   PM User | #7
starmandell
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
starmandell is an unknown quantity at this point
cause i know the exact format of the input, and its going to be like t05:34:23-79-79.003z.444z. its on a local server and it needs to have the : in it
starmandell is offline   Reply With Quote
Old 08-09-2010, 02:41 AM   PM User | #8
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

Quote:
but the encoded version still gets passed into the url. passing in t05:34:23-79-79.003z.444z into the input box still results in http://localhost/Lights/WorkOrder.js...9-79.003z.444z in internet explorer.
Yep, thats exactly what I got in IE and FF which is why I said it's NOT a COMPLETE answer to your request but it may help you see what's going on.

See post at #5.

LT

Last edited by low tech; 08-09-2010 at 02:44 AM..
low tech is offline   Reply With Quote
Old 08-09-2010, 03:30 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by starmandell View Post
i tried running the script, but the encoded version still gets passed into the url. passing in t05:34:23-79-79.003z.444z into the input box still results in http://localhost/Lights/WorkOrder.js...9-79.003z.444z in internet explorer.
And I say again: You *WANT* the URL to *APPEAR* like that! IF IT DOES NOT then the URL will *NOT* submit properly to your server!!!

Your JSP page COULD AND SHOULD properly handle that encoding!

In particular, the JSP page should do
Code:
String lightid = request.getParameter("lightId");
and the String that results *WILL* be decoded correctly.

The HTTP specification *REQUIRES* the correct encoding of query string values! If the query string *appears* unencoded in the browser's address bar, that may just be appearance. In point of fact, the query string as it will be sent to the server could and should be encoded.

Basically, any character that could be mistaken to be part of the standard HTTP "meta" information should be encoded. That means / : ? & % space + and more. There is not ANY gurantee that a non-encoded URL is going to work and/or work properly.

ANYWAY...If your JSP code isn't handling that encoding properly, then your JSP code has a bug in it. Period.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 08-09-2010 at 03:37 AM..
Old Pedant is online now   Reply With Quote
Old 08-09-2010, 03:36 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
In short, you should *NOT* have any decode() function in that code and you should *NOT* be attempting to undo what the browser is CORRECTLY doing.

Your <form> should consist ONLY of
Code:
<form name = "workorder" action="http://localhost/Lights/WorkOrder.jsp" onsubmit="return validate_form(this)" method="get">
<input type="hidden" name="method" value="getWorkOrders" /> 
<b>WorkOrder:</b>
<br> Id Required:  <input type="text" name="lightId" size="30" />
<input type="submit" value="Go"/>
<br>
</form>
You don't show what your validate_form() function is doing, but it clearly should not be trying to encode anything.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 08-09-2010, 05:05 AM   PM User | #11
starmandell
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
starmandell is an unknown quantity at this point
oh okay. ill keep that in mind. thank you!
starmandell is offline   Reply With Quote
Reply

Bookmarks

Tags
decode, encode, input, javascript, url

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 AM.


Advertisement
Log in to turn off these ads.