SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down adding tags using String.replace()

    When trying to emulate php's nl2br() function in javascript, I ran into something quite disturbing.

    Code:
    cms.t.value = cms.t.value.replace(/\n/gi, '<br />\n');
    Using this piece of code, I was really surprised that the newlines weren't preceded by <br />, but by <br>. I really don't understand why that would happen. Why can't I decide my own replacement string? .

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand it either, this works just fine for me
    Code:
    <html>
    <head>
    	<title>Test</title>
    	<script type="text/javascript">
    		String.prototype.nl2br = function()
    		{
    			return this.replace( /\n/g, '<br/>\n' );
    		}
    	</script>
    <body>
    <form id="cms" action="" method="get">
    	<textarea name="t"></textarea>
    	<input type="button" value="go" onclick="this.form.t.value=this.form.t.value.nl2br();" />
    </form>
    </body>
    </html>
    What browser are you using? Do you have this page online?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Sure, I've put the pages here: http://www.cs.vu.nl/~sgbouwhu/cms/

    I've tried it with both Mozilla (Firebird) and MSIE6, and both browsers behaved the same, that's what surprised me most..

    It should become a semi-graphical interface for posting blogs. Click on the gray div and it turns into a textarea. Click outside the textarea and it should become a div, with the textarea's value parsed. Newlines in the textarea should automatically convert to <br />s, to do that, put a few newlines in the textarea, click outside of it and than click back on it. Newlines are then replaced by <br> (mozilla) or <BR> (IE).

    Your example worked fine for me too, by the way. But I haven't yet been able to find the difference..

    Feel free to comment on it by the way, this is my first "Learning javascript" try .

  4. #4
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, and by the way: IE is acting really strange when updating the content, hiding everything and showing each element on the page only when hovering over it. Now this is not a huge problem, because I use Mozilla primarily and it's going to be for private use, but still. Anybody has a clue how that happens, and/or how to prevent it from happening?

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your function works properly. It's the innerHTML property that's goofing you. It will accurately return the HTML, but it's never proper XHTML.

    Don't know of any fix to this other than to parse the innerHTML property into the XHTML you wish to see, such as

    innerHTML.replace( /<(br|hr)>/g, "<$1/>" );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    !

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe. You're welcome.

    Just lemme know if there's anything else.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is. Check out post #4.

    Oh, and also: I'm assigning event handlers for onclick and onblur events, executing functions from the cms class. In those functions, I can't use this.functionName() but I have to use cms.functionName(). This means the object can only be constructed correctly when it's named "cms". Any workaround for that? (another reference to the same object that I can use, or something?)

    Hmm, I think this sounds kinda vague. But if you look at the updateTextbox and updateDiv methods I hope you understand wht I mean..

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya

    var self = this;

    inside the method, then use self inside your event handlers.

    Not sure about #4 -- I'll have to think about that. But, might want to check this thing I've been writing for a possible, alternate, solution.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I'll check your project out, but I don't think I'll use it, the whole reason I started this thing is because Midas' functionality was too limited for me. But it might help me out if I become stuck again in the future .

    Thanks for your help.

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, yes. I remember. Actually, it's not Midas that's limited, it's the designMode interface that's lacking.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  12. #12
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <click>Box goes big...<click>box goes small...<click>box goes big...<click>box goes small...

    I don't know why, but that entertains me no end

    There are problems in your code however if you click on Link or Image and then - without entering anything - click on Cancel until the prompt window disappears.


    Andy
    From the English nation to a US location.

  13. #13
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Netherlands
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know, it's far from finished yet. But as I said, it's a learning project for me so I don't expect it to work flawlessly yet .


Bookmarks

Posting Permissions

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