Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

single quote --> double quote

Status
Not open for further replies.

VBmim

Programmer
Jun 25, 2001
361
BE
Hello

I have something very strange going on...

I get some info from a sql server database via remote scripting in javascript. Here is the code for the function in my remote scripting page:

Code:
function GetDescr(ArtNo)
{
conn = Server.CreateObject("ADODB.Connection");
conn.ConnectionTimeout = Application("C_ConnectionTimeout")
conn.CommandTimeout = Application("C_CommandTimeout")
conn.Open(Application("C_ConnectionString"), Application("C_RuntimeUserName"), Application("C_RuntimePassword"));

var rsDescr=conn.execute("select Dutch, French from PData where Art='" + ArtNo + "'")
var Descr;
if (rsDescr.eof == false)
{
Ned = rsDescr("Dutch").value;
Fra = rsDescr("French").value;
}
else
{Ned= "empty";
Fra= "empty";
}
Descr= {Ned:Ned,Fra:Fra};
return Descr;
}

Here's the code where the function is used

Code:
rsFunc = RSGetASPObject("rsInputFunctions.ASP");
Descrip = rsFunc.GetDescr(form2.Art.value).return_value;
document.getElementById("DescrD").value = Descrip.Ned;
document.getElementById("DescrF").value = Descrip.Fra;

The data is allright, except when there is a single quote in the fields 'Dutch' or 'French'. Somehow it always gets converted into double quotes, where I would like to convert them myself to 2 single quotes.

The statement
Code:
Ned = rsDescr("Dutch").value.replace("'","''")
changes the single quote into one double quote and one single quote.

How is that possible, and what can I do about it?

Greetz

VBMim
 

Is the quote transformation definately taking place in the JavaScript? Is it possible that SQL is returning the data to your code with the quotes already transformed?

Dan
 
Hello Dan

In fact I am converting a data-input page written in asp to javascript and the asp code returns the strings with the single quotes all-right... so I suppose it's javascript doing this.

Greetz

Mim
 
try this:
Ned = rsDescr("Dutch").value.replace(/'/g,"''")

Known is handfull, Unknown is worldfull
 
Hey vbkris

I tried it, but it gave me a double quote and single quote...

Greetz

VBMim
 
doesnt make sense...

the value that is returned is "Dutch" while it should be 'Dutch'???

try this to:
Ned = rsDescr("Dutch").value.replace(/"/g,"'")
Replacing of " with '

Known is handfull, Unknown is worldfull
 
The value Mike's Place is returned Mike"s Place.

This code is part of a data-input page. So you can retrieve as well as save data.
When I fill up all my text variables when the user queried for one article, the user can change the field he/she wants and then save it. But if the ' is changed into " it isn't correct...

The line of code you gave me resulted in one double quote instead of a single quote...

Thanks for your time allready

Greetz

Mim
 
>> In fact I am converting a data-input page written in asp to javascript

Given that (and please correct me if I'm wrong) ASP isn't a language in its own right, and that you can write ASP pages *in* JavaScript, I'm not quite sure how you'd "convert ASP to JavaScript". Do you possibly mean "convert an ASP page written in VBScript to one written in JavaScript"?

Either way, I still can see nothing in your code that would explain why the quotes are being converted. I have written many ASP pages in JavaScript that retrieve values from an MS SQL database - and all single and double quotes are returned as I would expect... So to that end, is there any code you're not showing us?

We'll get there in the end!

Dan
 


Given that (and please correct me if I'm wrong) ASP isn't a language in its own right, and that you can write ASP pages *in* JavaScript, I'm not quite sure how you'd "convert ASP to JavaScript". Do you possibly mean "convert an ASP page written in VBScript to one written in JavaScript"?
Yup, that's it exactly


I don't realy understand it either... I have never encountered this before, and I have had some experience with javascript and data...

There must be something I am overlooking here...
Well... my working day is over now, tomorow is a brand new day and maybe with a fresh look on the situation I'll find out what's wrong...

Greetz
Mim
 
maybe u should convert the " to ' in javascript

Known is handfull, Unknown is worldfull
 
Yello

I found out something new.
If I use
Code:
Descr=Ned;
return Descr;

instead of
Code:
Descr= {Ned:Ned,Fra:Fra};
return Descr;

the value is returned the way it should be...

The question of course is why? And what can be done about it...
 
Maybe because { blah:blah } creates an object... and RS does some pretty nasty things with object serialization.

What happens when you use plain JS array, e.g. Descr = new Array(Ned, Fra); ?
 
This is a strange one. If I knock up a test harness, without your database stuff and prototyped methods:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function GetDescr() {
			var Descr;
			Ned = "The cat sat on the mat";
			Fra = "The cat didn't sit on the mat";
			Descr= {Ned:Ned,Fra:Fra};
			return Descr;
		}

		function testMe() {
			Descrip = GetDescr();
			alert(Descrip.Ned);
			alert(Descrip.Fra);
		}
	//-->
	</script>
</head>

<body onload="testMe();">
</body>
</html>

it works fine.

I cannot help but notice the following line in your code:

Code:
Descrip = rsFunc.GetDescr(form2.Art.value).return_value;

Assuming that the GetDescr method of the rsFunc object you are calling is the same as the one above, then I'm wondering what the ".return_value" bit is for... The GetDescr function you've given above doesn't appear to return any property with that name - so where does it come from? Is there other code to populate this that may be affecting things?

Dan
 
Hello

-vongrunt-
Is there some documentation about the 'pretty nasty' things that rs does?
What happens when you use plain JS array, e.g. Descr = new Array(Ned, Fra); ?
I tried that, and to my big surprise it had the same outcome, double quotes... I was realy expecting single ones.

-BillyRayPreachersSon-

I also tried the code you provided me, and if I test, it comes out right allready (with the single quotes and all).

Assuming that the GetDescr method of the rsFunc object you are calling is the same as the one above, then I'm wondering what the ".return_value" bit is for... The GetDescr function you've given above doesn't appear to return any property with that name - so where does it come from? Is there other code to populate this that may be affecting things?

the '.return_value' is a property of the remote scripting call, and returns the return value of the function you called.


I'll just have to keep on searching... Damned those quotes!
:)

 

>> the '.return_value' is a property of the remote scripting call, and returns the return value of the function you called.

Have you tried removing it? From the code you've given, it doesn't look like it is needed.

Hope this helps,
Dan
 
This was based upon my experience with RS.

It works fine when remote function returns scalar value (string, number, blah). For objects (arrays included) it marks response as EVAL_OBJECT (for eval()uation client-side) and encodes each element for marshalling. Two problems here:

- single quotes got replaced with \"
- double quotes cause runtime error in eval() client-side.

Anyway, the following functions are responsible for encoding/decoding:

RS.htm: _MSRS_evaluateRequest()
RS.asp: unevalString()
 

Code:
function unevalString(str) 
{	return '"' + str.replace(/([^\\])'/g,'$1\\"') + '"'; 	}

This is the code I found in rs.asp...
Although I didn't find it a good idea (and I have a hard time understanding what replaces what with this function) I changed the function to

Code:
{	return '"' + str.replace(/([^\\])'/g,'$1\\'') + '"'; 	}

but it didn't make any difference.

Anyhow, I think I have some kind of solution...
The guy who wrote the app originaly probably encountered the same problem because I found following function in another remote scripting page:

Code:
function fixstring(str) 
	{
if (str==null) return '';
var ret=str;
var pos=0;
while ((ret.charAt(0)<'!')&&(ret.length>0)) ret=ret.substring(1, ret.length);
while ((ret.length>0)&&(ret.charAt(ret.length-1)<'!')) ret=ret.substring(0, ret.length-1);
while ((pos<ret.length)&&(ret.length>0)) {
while ((ret.charAt(pos)<' ')&&(ret.length>0)) 
ret=ret.substring(0,pos)+ret.substring(pos+1,ret.length);
pos++;
}
while (ret.indexOf('"')!=-1) {
ret=ret.substring(0,ret.indexOf('"'))+'&#34;+ret.substring(ret.indexOf('"')+1,ret.length);
}
while (ret.indexOf("'")!=-1) {
ret=ret.substring(0,ret.indexOf("'"))+'&#39;+ret.substring(ret.indexOf("'")+1,ret.length);
}
if (ret.length==0) return '';
return ret;
}
[code]

This function replaces ' by &#39; and " by &#34;. It's a messy solution but I don't have any better yet.
 
return '"' + str.replace(/'/g,'\\"')

>> Replaces ' widh a " and '

what r u looking for???

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top