www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help with string manipulation

  1. #1
    Join Date
    Feb 2006
    Posts
    66

    Help with string manipulation

    Hey

    Im having difficulty with editing a div inner html.

    Im trying to split it on the
    <br>
    then only show the latest 4 lines but it won't work.


    This is what i got:

    PHP Code:
                                    var original =     function Chat(divurl) {
            
    this.div div;
            
    this.url url;
            
    this.start = new Date().getTime();

            
    this.start = function(lastupdate) {
                
    lastupdate lastupdate lastupdate : new Date().getTime()
                
    ajaxPost(this.url'lastupdate='lastupdate, function(up) {
                    return function() {
                        if (
    this.readyState == 4) {
                            
    // success
                            
    if(this.status == 200) {
                                var 
    data = eval('(' this.responseText ')');
                            
                                
    // start next timer
                                
    up.start(data.lastupdate);
                            
                                
    // fill chat
                                
    //STRING MANIPULATION HERE
                                    
    var original document.getElementById(up.div).innerHTML;
                                    var 
    index original.indexOf('<br>') + 4;
                                    var 
    partial original.substr(index);
                                    
    document.getElementById(up.div).innerHTML partial data.html;

                        }
                            
                            
    // connection error
                            
    else {
                                
    // try again in .8 of a seconds
                                
    setTimeout(function(){
                                    
    up.start(lastupdate);
                                }, 
    800);
                            }
                        }
                    };
                }(
    this));
            };
        }
        
        var 
    chat = new Chat('chat''getnewmessages.php'); 

    The script works fine except trying to edit the string to only show a max amount of lines. How can i get this work ?

    Please help!
    Thanks

  2. #2
    Join Date
    Mar 2009
    Posts
    501
    If you have a single string (like an element's innerHTML property) AND they can be parsed into lines because they have a single token/string separator such as '<br />' you can do this by running the string through a function like this:

    Code:
    function onlyNewStuff( numLines, innerHTMLString){
        var x = innerHTMLString.split( '<br />');
        while( x.length > numLines){
              x.shift();
         }
          x = x.join('<br />');
          return x;
    }
    So, if you had a div that contained all the lines, and you wanted to rid it of all but the last four lines, you would do something like this with it:

    Code:
    var a = document.getElementById('HTML_ID_OF_THE_DIV_TO_BE_PURGED');
    a.innerHTML = onlyNewStuff(4, a);
    That should do it.

  3. #3
    Join Date
    Feb 2006
    Posts
    66
    Thanks!

  4. #4
    Join Date
    Mar 2009
    Posts
    501
    Sorry---
    The last block of code should read:
    Code:
    var a=document.getElementById('HTML_ID_OF_THE_DIV_TO_BE_PURGED');
    a.innerHTML = onlyNewStuff(4, a.innerHTML);
    Sorry.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles