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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can someone tell me why this line does not work in Mozilla browsers?

Status
Not open for further replies.

mcmcom89

Programmer
Nov 23, 2005
7
CA
function displayHTML(form) {
var inf = form.txtStory.value;
var tmp = inf.replace(new RegExp(/\r\n/g),"<br>");
win = window.open(", ", 'popup', 'toolbar = no, status = no, height=500 width=410 resizable=yes, scrollbars=yes');
win.document.write("<font face='arial' size=2><b>Preview your message:</b><br><br>" + tmp + "<br><br><a href='#' onclick='window.close()'><img border=0 src='images/close_button.gif'></a></font>");
}

its writing out but the Replace function does not replace the newline breaks with <BR>

thanks
 
there are a few things wrong with this.

1) you should always use references in this manner:
Code:
var inf = form.elements['txtStory'].value;
but i'm sure you will ignore that advice.

2) your regex needs escaped backslashes, because backslashes in themselves are escape characters. try this:
Code:
var tmp = inf.replace(new RegExp(/[red]\[/red]\r[red]\[/red]\n/g),"<br>");

3) you need commas in between each window characteristic, like this:
Code:
'toolbar = no, status = no, height=500[red],[/red] width=410[red],[/red] resizable=yes, scrollbars=yes'

try all that. then let us know how it works.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
thanks i made the comma changes and changed the line to read form.elements['myelement'] as you said

but the regex still does not work.

if i use escaped chars (\\r\\n) it does not work for either IE or Mozilla,

if i use my original line it works in IE but not Mozilla

any ideas?

thanks
 
I'm not sure you can pass a regexp as a valid parameter to the RegExp constructor. It usually accepts 2 strings - first one is a string representation of the regexp (excluding the 2 /'s) and the 2nd is a string representation of the regular expression flags (g, i, and gi). Additionally, you do not have to escape your escape characters if you are not defining the regexp using the RegExp function (or in other words, declaring it as a string).

Either of these should work, but do not mix the 2:
Code:
var tmp = inf.replace(new RegExp("\\r\\n", "g"), "<br>");
-or-
var tmp = inf.replace(/\r\n/g, "<br>");
I prefer the 2nd method, but that's just me. I only use the fist method if I have to build the regexp dynamically - hence the need for a string.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
images
 
Oh, cory also makes a good point, I'm pretty sure that FF and IE interpret line breaks differently. I believe BabyJeffy did a test on this in another thread a few weeks ago. I think one interpreted a line break as \r\n and another interpreted it as just \n. (or some combination thereof)

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
images
 
/\r\n|\r|\n/g;

that works for me too!

thanks guys! and i will remove the font tags and use css ;)

lates.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top