Replace - JScript Version

pm2k
pm2k used Ask the Experts™
on
I need someone to translate the following code (VBScript) to JScript

<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
<%
var Address
Address = Request.Form("Address")
Address = Address.Replace(unescape(%0D%0A), "<BR>")
Address = Address.Replace(" ", "&nbsp;")
%>

Commented:
sorry, the Replace function in js should be "replace" (javascript is case sensitive)

<%
var Address;
Address = Request.Form("Address");
Address = Address.replace(unescape(%0D%0A), "<BR>");
Address = Address.replace(" ", "&nbsp;");
%>

Author

Commented:
Error Type:
Microsoft JScript compilation (0x800A03EA)
Syntax error
/johnson/EmployeeEdit.asp, line 55, column 35
Address = Address.replace(unescape(%0D%0A), "<BR>");
-----------------------------------------------^

Nop, I get the above error
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Commented:
give it some quotes

Address = Address.replace(unescape("%0D%0A"), "<BR>");

Author

Commented:
Error Type:
Microsoft JScript runtime (0x800A01B6)
Object doesn't support this property or method
/johnson/EmployeeEdit.asp, line 55

I get these error for the quotes I added in
like this?

var address = documen.*form*.Address.value;
address = address.replace("\n", "<br>");
address = address.replace(" ", "&nbsp");

Author

Commented:
Nop
I have tried address = address.replace("\n", "<BR>"); before this and I get the same error
Error Type:
Microsoft JScript runtime (0x800A01B6)
Object doesn't support this property or method
/johnson/EmployeeEdit.asp, line 55
whereby line 55 is address = address.replace("\n", "<BR>");
oops ... document, not documen

var address = document.*form*.Address.value;

I should also point out that you need to replace *form* with the ... id or name ... I can't remember which ... of your form.  Just set both.  Then you can't go wrong :)

<form name="frm" id="frm" method="post" action="aoeu.asp">
...
...
...
var address = document.frm.Address.value;
Are you checking for null?

if (address != null){
   yer code
}

This is all about replace
http://www.devguru.com/Technologies/ecmascript/quickref/string_replace.html

var aa = "123456789"
aa = aa.replace("6", "9");
document.write(aa);

This works, I just tested it.  Make sure that you have gotten the value out of the text box.  Try this.  The alert function makes a box pop up.  Try it.  If the box doesn't have the value of address in it, then you never got it from the text box.  Also, this code must be on the SAME page as the text box and be processed BEFORE submitting the form.

var address = document.*form*.Address.value;
alert(address);
address = address.replace("\n", "<br>");
address = address.replace(" ", "&nbsp");

Author

Commented:
Nop.
I think you get me wrong. Let me explain in advance.
I'm actually passing the value from a.asp to b.asp
and I have confirm that I can successfully pass the value to b.asp
but when it come to replacing the <BR> and &nbsp; I'm stuck
but I know that (VBScript version)
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>
works fine.
The only thing I want is a similiar prototye as above in ASP-JScript
Thanks
Ya, I got it.

Change your submit button to
<input type="button" onsubmit="return submitMe()">


...


function submitMe()
{
   document.*form*.Address.value.replace("\n", "<br>");
   document.*form*.Address.value.replace(" ", "&nbsp");
   document.*form*.submit();
   return true;
}

that should do it!
sorry ... gawd

<input type="button" onclick="return submitMe()">

Author

Commented:
erm...
what I want is the ASP/JScript version
meaning
<%@ Language = JScript %>
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>
Something like this in JScript
try this out

<%
var Address = String(Request.Form("Address"));
Address = Address.replace(/ /g, "&nbsp;");

var i = 0;
var sNewAdd = "";
for(i=0; i<Address.length; i++)
{
      if(Address.charCodeAt(i) == 13)
           sNewAdd += "<br>";
      else
           sNewAdd += Address.charAt(i);
}

%>

Here sNewAdd will contain the desired string u want....
why js instead vbs?

Commented:
because vbs dont work for all browsers whereas jscript does.
howtodo, do not mix up server side scripting with client side scripting.


in example to client will be send NOTHING
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>

But if you use these variables with response, then client become the value:

<%=Address%>
Or:
<%Response.Write Address%>





Commented:
you are not getting what i meant!

vbscript is also client side scripting so what difference does it make to use
javascript over vbscript, i assume that's ur question.

So what im trying to say is that vbscript is not supported by all
browsers. Example Unix macs or maybe even netscape they dun support vbscriptin.
So javascript is usually more powerful than vbscripting, get it?

and ur codes:
=============================
in example to client will be send NOTHING
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>

But if you use these variables with response, then client become the value:

<%=Address%>
Or:
<%Response.Write Address%>
=================================
is definitely not what pm2k wants.

* and sorry pm2k for discussins something off topic on ur post.

Commented:
plus... i supposed he's using that for a client side manipulation.
howtodo, remeber if you see <%%>, then is ONLY server side scripting.

and if you see <script></script> then is for client.
now you can read topic again, and decide who is not getting ;)




Commented:
very well, i mis read the questions.
But what i wrote is still true rite? if its for client side scripting?

Author

Commented:
khimanijay; you method also returning the same error
Microsoft JScript runtime (0x800A01B6)
Object doesn't support this property or method
/johnson/EmployeeEdit.asp, line 53
The problem always occur whenever I tried to use the .replace()

and hotodo; no worry I like discussion like this anyway.
Btw do you have any idea on how to solve that?
yes is still true, VBS is only for Internet Explorer.

this Message Box will be see only IE user

<script  language="VBScript">
  MsgBox("hello word")
</script>

and this for "all"
<script>
  alert("hello word")
</script>

pm2k  can you answer why your try js instead vbs?

Author

Commented:
I have no choice devic.
This is my assignment and was required to develop it using ASP/JScript.
Futhermore every PC in the lab (except a few new PC) doesn't have IE and are currently using Netscape.
pm2k
i use always ASP/VBS for server and for client scripts JS. Some times, if i need only for IE, then VBS too.

i think you must read good, what i wrote above.
it doesn't matter what browsers are in the lab. Server return clear HTML.

Back to our example:
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>

what do you want to do with Address?
you want to send to client, because you prepare html.
If you get from some form Address ="My address:
my City
my Street
"
after replase Address will be "My&nbsp;address:<BR>my&nbsp;City<BR>my<BR>Street"

and if you write like this:
<%=Address %>

Then client become only this "My&nbsp;address:<BR>my&nbsp;City<BR>my<BR>Street"
and is for ALL browsers.

And then, if you need, you can use client scripting too.

ASP/VBS + JS

Author

Commented:
Okay I get what you mean here.
But as I mentioned, I have no other choice as this is my assignment and I have to use ASP/JScript to complete it.
The reason why I wanted to
<%
Dim Address
Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")
%>
is because I need to store the value into database.
Hope you understand my situation.
ok, the problem is that your boss does mix up server side scripting with client side scripting, and for browsers compatibility he is searching for BEST SOLUTION :)

back to example:


you need Address to store  into database. Is SERVER SIDE OPERATION!

good luck in your search!



Soo

Commented:
Thanks for your advice devic.
I'm actually a student who have to complete an simple assignment using JScript as order by lecturer.
I think there is an misunderstanding between us.
I'm aware of the client and server side scripting but our lecturer request for the JScript version of assignment to be submited, that's the reason why I didn't go for VBs. Hope this clear the misunderstanding between us.
Thanks anyway. Have a nice day!

Commented:
?!?:

Address = Request.Form("Address")
Address = Replace(Address, vbCrLf, "<BR>")
Address = Replace(Address, " ", "&nbsp;")

Instead of doing that, use Server.HTMLEncode() which does the same thing for you.

That solves a lot of other problems for you as well - that's why it's built into ASP.

Commented:
This is what I found at:  
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsmthreplace.asp

r = ss.replace(re, "A");  

where: var ss is the string containing text to replace
            var re contains expression you want to replace
            and "A" is what you want to replace it with

I assume you could just replace var re with the expression itself

Regarding the previous debate about jscript vs vbscript - I assume it is merely an intellectual question on behalf of the lecturer, because I can't see any reason ever to use jscript with ASP unless you are already familiar with it when you learn ASP. When using ASP the user's browser has no bearing on whether vbscript will run or not because it is never run on the persons browser.

All my pages are a mixture of javascript for clientside and vbscript for serverside (though I don't know javascript that well).

But your lecturer is giving you the marks and wants you to know even though you will probably never use it. (Might give a head start for learning jsp though)

Commented:
P.S.
Server.HTMLEncode() is easier to use I think than Replace but it can't handle replacing apostrophes (to the best of my knowledge).
Commented:
Sure it does. Server.HTMLEncode() is used to _display_ values in HTML without breaking it - and can also be used in JScript.

The replacing of single quotes you're probably referring to is the single quotes that mess up SQL Server. In that case you want to comment them with another, ONLY when used in a SQL statement. Otherwise you're changing the value of the data when you shouldn't!

Example in JScript:

<% @Language = "JScript" %>
<%
String.prototype.encodeBreak = function()
{
      return this.replace(/\n/g, "<br/>\n");
}

var address = "";
address += "123 Main Street\n";
address += "Hoboken, NJ\n";
address += "12345";
address = Server.HTMLEncode(address).encodeBreak();
Response.write(address);
%>

Author

Commented:
<%@ Language = JScript %>
<%
  var oConn, Rs, Sql = new String;
  var Address;
  String.prototype.encodeBreak = function()
  {
    return this.replace(/\n/g, "&lt;br/&gt;\n");
  }
  Address = Server.HTMLEncode(Form("TEXTAREA1")).encodeBreak();
  oConn = Server.CreateObject("ADODB.Connection");
  oConn.Open("DSN=tyeyo; userId=tyeyo; pwd=tye0407");
  Sql = "Insert Into a (asd) Values ('" + Address + "');";
  Rs = Server.CreateObject("ADODB.Recordset");
  Rs.Open(Sql, oConn);
%>

I get this error
Error Type:
Microsoft JScript runtime (0x800A138F)
Object expected
can you pls help me whammy?

Author

Commented:
And the error is on this line
Address = Server.HTMLEncode(Form("TEXTAREA1")).encodeBreak();

Commented:
Shouldn't that be:

Address = Server.HTMLEncode(Request.form("TEXTAREA1")).encodeBreak(); //?

I'm not totally sure of server-side JScript syntax, I had to tweak the above a bit to get it to work.

Commented:
P.S. Please tell me you're not doing this client-side with VBScript.

Commented:
I meant JScript - doh. However, either one are proprietary ©Microsoft languages, and don't work in hardly anything that wasn't created by ©Microsoft.

If you're trying to do this client-side, we need to use real JavaScript, and hack it for ©Microsoft browsers if you have to.

Author

Commented:
Thanks!

Commented:
Whoa... I guess you got it working?

Author

Commented:
Indeed
Thanks alot whammy!

Commented:
Whammy

I was indeed talking about "the single quotes that mess up SQL Server".

To get rid of these I have been using replace and replacing them with two single qoutes going into the dbase and then swapping them round on the way out.

Is that what you mean by "commenting them with another"?

Jenny

Commented:
Yes, pretty much. :)

Commented:
TA!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial