Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
    		}
    	}

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 02-13-2013 at 04:45 PM.

    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.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Last edited by felgall; 02-14-2013 at 08:02 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •