Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2013, 01:30 PM   PM User | #1
klads
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
klads is an unknown quantity at this point
Javascript replace()

A small part of my web site allows a user to enter a warning message that will appear on the login html page.

The message is captured in a textarea input field. Once the form is submitted a Java process replaces any occurences of '\r\n' with </br> and saves the text to a .txt file on the web server.

The login html page then uses a bit of Ajax to retrieve the .txt file and displays the content it if not empty.

This all works perfecty in both IE7 and Firefox 18.0.2.

To allow the user to amend the message I wish to read it from the web server and display it in the same textarea input field. Prior to display I am attempting to replace the </br> with '\r\n'.

This works in Firefox and but IE7 seems to replace the </br> with space. My Javascript code is as follows. Any help would be greatly appreciated.

Code:
	var http = createRequestObject();

	function createRequestObject() {
		var objAjax;
		var browser = navigator.appName;
		if (browser == "Microsoft Internet Explorer") {
			objAjax = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			objAjax = new XMLHttpRequest();
		}
		return objAjax;
	}

	function getWarningMessage(){
		var rightNow = new Date();
		http.open('get','/warningmessage/warningmessage.txt?' + rightNow.getTime());
		http.onreadystatechange = updateNewContent;
		http.send(null);
		return false;
	}

	function updateNewContent(){
		var warningMessage;
		var warningMessageReplace;
		if (http.readyState == 4 && http.responseText.length > 0){
			warningMessage = http.responseText;
			while (warningMessage.search('</br>') > 0) {
				warningMessageReplace = warningMessage.replace('</br>','\r\n');
				warningMessage = warningMessageReplace;
			}
			document.getElementById('warningMessage').innerHTML = warningMessage;
		}
	}
klads is offline   Reply With Quote
Old 02-13-2013, 04:02 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You need to make the regex case-insensitive for IE. I am surprised that your code works in any browser as / must be escaped with \.

Code:
warningMessageReplace = warningMessage.replace(/<\/br>/gi,'\r\n');
If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-13-2013 at 04:45 PM..
Philip M is offline   Reply With Quote
Old 02-13-2013, 06:32 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
What is the purpose of:

Code:
if (browser == "Microsoft Internet Explorer") {
			objAjax = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			objAjax = new XMLHttpRequest();
		}
All browsers now recognise XMLHttpRequest and the navigator.appName can contain "Microsoft Internet Explorer" in Firefox, Opera, Chrome or Safari masquerading as Internet Explorer and Intertnet Explorer can be set to identify itself as any other browser. Even if you do need to include the activeX call to support the dead IE6 browser then there are more accurate ways to difect that call specifically to IE4 through IE6 that don't interfere with other browsers being able to use the call that they recognise.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-13-2013, 10:58 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
As Felgall said.

One better way (and there are others, but this works for me) is:
Code:
function createRequestObject() {
    return   ( XMLHttpRequest != null )
           ? new XMLHttpRequest( ) 
           : new ActiveXObject("Microsoft.XMLHTTP");
}
Another seemingly popular way:
Code:
function createRequestObject() 
{
    try {
        return  new XMLHttpRequest();
    } catch ( ignored ) { }
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
   } catch ( oops ) {
        return null;
   }
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-14-2013, 07:59 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
As Felgall said.

One better way (and there are others, but this works for me) is:
Code:
function createRequestObject() {
    return   ( XMLHttpRequest != null )
           ? new XMLHttpRequest( ) 
           : new ActiveXObject("Microsoft.XMLHTTP");
}
Another seemingly popular way:
Code:
function createRequestObject() 
{
    try {
        return  new XMLHttpRequest();
    } catch ( ignored ) { }
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
   } catch ( oops ) {
        return null;
   }
}
Of course with IE6 being dead you can even drop the activeX reference completely and just use new XMLHttpRequest() since all the browsers people current use support it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 02-14-2013 at 08:02 AM..
felgall is offline   Reply With Quote
Old 02-14-2013, 02:33 PM   PM User | #6
klads
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
klads is an unknown quantity at this point
Although IE6 may be "dead" to the wider browsing community...it is still alive and well in my company and, as a such, still needs supported. Hopefully not for long but that's the position I'm in.
klads is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 PM.


Advertisement
Log in to turn off these ads.