...

View Full Version : Passing special characters to javascript functions



huzefahakim
07-11-2002, 09:29 PM
I am trying to create a URL through Javascript function and then use that url to open a new window.

I pass the location, arguments and values so as to form a url such as
location?arg1=value1&arg2=value2.....

When I try to pass a value as 'a_123%CDE_GHI%KL', the function receives the value as 'a_123E_GHI%KL' .

I have tried combinations of escape and escape before and after sending values to the function, but it does not work.

Can someone help me with the right way to send these values to the function and thus
a. form the correct url with escaped values
b. retrieve the original unescaped value in the new window.

Huzefa.

poccil
07-11-2002, 10:44 PM
Unless I'm mistaken, percentage symbols are disallowed in URLs because of their role in escaping foreign characters (including itself).

There is a JavaScript function named escape that performs this escaping work. Call it before passing the URL value.

Any '%' in a string passed to escape will be replaced with "%25".

The reason why a string like "a_123%CDE_GHI%KL" will not be the same when unescaped by the browser is because of the two characters that follow the first '%'. Since 'CD' is a valid combination of hexadecimal characters, the "%CD" part of the string is replaced by its represented character, ''.

More information on escape:


[escape Function]

Encodes [strings] so they can be read on all computers.

[Syntax:]
escape(charString)

The required charString argument is any [string] to be encoded.

Remarks
The escape method returns a string value ... that contains the contents of charString. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."

huzefahakim
07-11-2002, 11:08 PM
I realize this, so I try to escape the strings before form the URL. But the function that does the escape receives the wrong value.

Have a look at this

Here are the two files

1. Test1. html

2. test.jsp

Notice the behaviour of substring '%CD' in the first string compared to the other % in both the links

Huzefa

test1.html

<html>
<head>
<script language="Javascript1.2">
function getEscapedUrl(url, arg1, value1,arg2,value2,arg3,value3){

document.write('Orginal Value is /a_123%CDE_GHI%KL/ <br><br> Value received from the function is '+value3);
alert("Original Value = /a_123%CDE_GHI%KL/ ) & Received Value = "+value3);
completeUrl=escape(url)+'?'+escape(arg1)+'='+escape(value1)+'&'+escape(arg2)+'='+escape(value2)+'&'+escape(arg3)+'='+escape(value3);
newWindow = window.open(completeUrl,'blank','width=200,height=200,scrollbars=yes,menubar=no,toolbar=no,resizable =yes');

return completeUrl;

}


function openNewPage( newurl, newtarget , width , height, toolbar){
newWindow = window.open(newurl,newtarget,'width='+width+',height='+height+',scrollbars=yes,menubar='+toolbar+',t oolbar='+toolbar+',resizable=yes');
newWindow.focus();
}
</script>
</head>
<body>

<b>

Get the escaped url of<br>
</b>

1. <A href="javascript: openNewPage(getEscapedUrl('test.jsp','serverName','webtest@SQA1','type','img_hit','id','/a_123%CDE_GHI%KL/'),'blank','200','200','no')">/a_123%CDE_GHI%KL/ </a>- Works only in Netscape<br>

2. <A href="javascript: openNewPage(getEscapedUrl('test.jsp','serverName','webtest@SQA1','type','img_hit','id','/GTE%ST4_GHI/'),'blank','200','200','no')">/GTE%ST4_GHI/</a> - works fine in IE and Netscape

</body>
</html>


test.jsp
<%
out.println("Value of Parameter ID from the request = " + request.getParameter("id"));
%>

Soldier Bob
07-11-2002, 11:19 PM
First off in IE i get a error

line: 11
char: 96
Error: Unterminated String constant

from the page... havent bothered to figure out what that is..

Anyhow, this code will submit data to a page like a jsp. It might be a simpler approach then what I see you doing. Adding a target="blank" to the form tag can allow you to send the results of a form submission to a new window I believe.

But dont use 'blank' _blank is a reserved word ... and well use something like 'TestWindow' if you really have to...

/a_123%CDE_GHI%KL/ gets turned into
test102.html?txtTestData=%2Fa_123%25CDE_GHI%25KL%2F


test102.html


<html>
<head>
<script type="text/javascript" language="javascript">
function process_data( the_data) {
alert( the_data );
document.frmTest.action="test102.html";
document.frmTest.submit();
}
</script>
</head>
<body>
<form name="frmTest" action="javascript:void(0);" method="get">
<input type="text" name="txtTestData" value="">
<input type="button" name="btnReadData" value="Process Data" onclick="process_data(document.frmTest.txtTestData.value);">
</form>
</body>
</html>


-S. Bob

huzefahakim
07-11-2002, 11:34 PM
well my problem is more complicated and I cannot use forms to submit. I need to form URLs to pass parameters.

In other words, when you pass the String '/a_123%CDE_GHI%KL/' to the following

function getEscapedUrl(value){
alert(value);
}

you would get the output as '/a_123E_GHI%KL/'.

Can someone help me with a way to get the original value back in the function before I can use it to escape.

Huzefa.

glenngv
07-12-2002, 04:58 AM
function getEscapedUrl(value){
alert(escape(value));
}



Originally posted by huzefahakim
well my problem is more complicated and I cannot use forms to submit. I need to form URLs to pass parameters.

In other words, when you pass the String '/a_123%CDE_GHI%KL/' to the following

function getEscapedUrl(value){
alert(value);
}

you would get the output as '/a_123E_GHI%KL/'.

Can someone help me with a way to get the original value back in the function before I can use it to escape.

Huzefa.

huzefahakim
07-12-2002, 05:45 PM
function getEscapedUrl(value){
alert(escape(value));
}

This function would give back the value
'/a_123%CDE_GHI%25KL/'

instead of '/a_123%CDE_GHI%KL/'.

Huzefa.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum