Getting message: Microsoft JScript runtime error '800a1391' 'vbCrLf' is undefined

Hi.  I'm using Classic ASP JS.  I'm trying to figure out the code to use for the "line break" in JS.  I used the string from one of my VBScript pages, but I get the error posted in the "Title" box above.

Here's the line:

<p><%=Replace(rsProdDetail.Fields.Item("ProdLongDesc").Value, vbCrLf, "<br>")%></p>


I've tried replacing the "vbCrLf" with "\n", but still get a similar error.

Could someone, please, help me with a JS-equivalent for this?


Thanks,
Shane
lshaneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:
lshane,

Did you try \n with quotes or without?  Did you get the same error message as in the question's title when you changed VbCrLf to \n or "\n"?

b0lsc0tt
peh803Commented:
Javascript uses "\n" for line breaks -- try that.

Thanks!
Phil
peh803Commented:
b0lsc0tt : sorry, obviously based on the identical timestamps of your post and mine, I didn't see yours before making my post!  No "point stealing" intended here...

Regards,
Phil / peh803
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

lshaneAuthor Commented:
Thanks for your speedy reply, b0lsc0tt.  I tried it both ways, actually.

Without quotes I get:
-----------------------------------------------------------------------------------------------------------------
Microsoft JScript compilation error '800a03f6'

Invalid character

/product_details.asp, line 196

Response.Write(Replace(rsProdDetail.Fields.Item("ProdLongDesc").Value, \n, "<br>"))
-----------------------------------------------------------------------------------------------------------------



With quotes I get:
-----------------------------------------------------------------------------------------------------------------

Microsoft JScript runtime error '800a138f'

Object expected

/product_details.asp, line 196

-----------------------------------------------------------------------------------------------------------------

"Line 196" is the code line:  
<p><%=Replace(rsProdDetail.Fields.Item("ProdLongDesc").Value, "\n", "<br>")%></p>


Your help is greatly appreciated,
Shane
b0lsc0ttIT ManagerCommented:
@peh803 - No problem.  Has happened plenty of times to me too and I'm glad that you posted to confirm \n for JavaScript.  It is not my expertise.  If our comments solve the problem then splitting is what I would do and I have no objection if that happens.

bol
peh803Commented:
try it this way instead:
   <p><%=Replace(rsProdDetail.Fields("ProdLongDesc").Value, "\n", "<br>")%></p>

Also, make sure your recordset object reference variable is spelled correctly.

It should be correct to use quotes, as above.

Regards,
Phil
lshaneAuthor Commented:
That's the way I tried a moment ago, and it rendered the following error message:

-----------------------------------------------------------------------------------------------------------------
Microsoft JScript runtime error '800a138f'

Object expected

/product_details.asp, line 196
-----------------------------------------------------------------------------------------------------------------

I know the spelling is correct, because if I remove all the "replace" jargon, it displays fine... it's just all "bunched up".  No carriage returns are being detected.  This very line (while using "vfCrLf") in my ASP VBScript page works fine.

Am I missing something in the syntax for JS?  I'm stumped.

Thanks,
Shane
b0lsc0ttIT ManagerCommented:
lshane,

Phil's line looked a little different than the one you used.  He suggested that you leave out .Item from the line.  That may not make a difference though and may have been unintentional.

Try replacing a different character to see if the problem is the \n or something else with the Replace statement.
lshaneAuthor Commented:
Hi, b0lsc0tt.  I actually just copied-and-pasted his line in my code.  Same error.


I really appreciate your efforts.  It just seems to me that there is always a parallel between VBScript and JS when using ASP, but this one seems to be difficult.
b0lsc0ttIT ManagerCommented:
In most cases you are correct but the replace in JScript may not be Replace.  Also JScript is probably case sensitive while VBScript is not.  The only equivalent that I have been able to find for VBScripts Replace is strObj.replace(rgExp, textToReplace).  Did you try my test?  Are you sure that Replace works in JScript?

If you aren't sure and want to try the replace method I used you could try something like ...

<%@ Language = JScript %>
var Description = rsProdDetail.Fields.Item("ProdLongDesc").Value;
Description = Description.replace("\n", "<br>");
%>

Notice that replace is lower case and the lines end with a semicolon.  You may already have those but just wanted to point them out.  If this doesn't work then maybe Phil has more experience with JScript or another expert.  It will help if we determine if the problem is replace or using \n.
peterxlaneCommented:
The replace function in JavaScript works differently than in VBScript.  It looks like this:

<%
var mystring = "hello world";
Response.Write(mystring.replace("h", "H"));
%>

And as I understand it, it uses a regular expression to do the replace, so you should be able to do the following:

<%
strJSCarriageReturn = /\n/g;
strDBField = rsProdDetail.Fields("ProdLongDesc").Value;
Response.Write(strDBField.replace(strJSCarriageReturn, "<br />"));
%>

Javascript is also case sensitive so it must be lower case.  The regular expression gets enclosed in "/" and then the "g" at the end indicates that it is a global search and replace, not just the first occurrence...







Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lshaneAuthor Commented:
Peterxlane - yours worked great.  Thank you.  Now that we have that resolved, I have one other question for you.

How can I "save" formatted text?  In other words, on the "Insert Item" form, in the "Administration" area, I enter the text, along with carriage returns, etc., and those display excellently now (thanks to your code); however, in that text, there are headings which I would like to bold.  How can I bold them in the "Insert Item" textarea, and have them displayed as bolded on the "Details" page on the live site?  Is there a relatively, feasible way to accomplish that?

Thanks so much,
Shane
peterxlaneCommented:
if the text has html in it and that is stored in the database, then it should retain that formatting when it is displayed on the page...

<b>hello world</b>

would show up bold

<h1>hello world</h1>

would show up as a heading


What am I missing?
houguanCommented:
To show formatted text, you need to user Server.HTMLEncode also

example:
<%
abc="<b>hello</b>"
Response.Write(abc&"<br>")
Response.Write(Server.HTMLEncode(abc))
%>

Output
====
<b>hello</b>
hello  <--in bold
peterxlaneCommented:
@houguan - That would actually produce the opposite of what you posted.  The first one would be bold and the second one would show the html tags... which means that you would not need to do anything special to get things to display in HTML.

lshaneAuthor Commented:
Hello, peterxlane and houguan.  Thanks so much for your diligence on this.  I'm still learning.

Actually, I know I could hand-code the HTML in the textarea field, however, I'm just wanting to copy-and-paste from something, such as MS Word... OR, I'd like to just press <CTRL-B> to bold the text, etc.  Is this possible with the "Server.HTMLEncode"?

In other words, if I copy a typed description from MS Word, which includes bolded headers, etc., then I would like that formatting to be maintained when I paste it into the textarea field of the "Insert Item" form.

How can that be accomplished?


Thanks so much,
Shane
peterxlaneCommented:
You would need some sort of online HTML editor.  Just Google "online HTML editor" and you will find a bunch of different products.  Some are definitely available for free and are pretty easy to implement.

lshaneAuthor Commented:
Hi, peterxlane.  That's kind of what I thought.  I was just hoping there was a way around it.  Hopefully I can find one that's simple to implement, as you suggested.

Thanks so much for your assistance.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.