Jump to content

Recommended Posts

Hey

 

Im using a js split to seperate lines then limit the amount to just 4 using slice but it won't do it.

 

The data involved according to firebug is:

 

{"lastupdate":1293751302,"html":"<a style=\"color:#AA0000;\" href=\"profile.php?view=1\">Username<\/a>: test <br\/>"}

My split string is this:

document.getElementById(up.div).innerHTML += data.html.split("<br/>").slice(0,3).join("<br/>");

 

But its not working - any one got a clue where its going wrong?

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/
Share on other sites

well i can give u the data involved so you try it :

 

div id is "chat"

 

var div = 'chat';

var html = 'username said hello five <br/>';

<div id="chat">

username said hello to you <br/>

username said hello to you two <br/>

username said hello to you three <br/>

username said hello to you four <br/>

</div>

 

 

This is it simplified for the above information:

document.getElementById(up.div).innerHTML += html.split("<br>").slice(0,3).join("<br/>");

 

1) First split on <br/>

2) From the split get array of 0 to 3

3) Join 0 to 3 with <br/> which we initially split on

4) The result should now show:

 

<div id="chat">

username said hello to you two <br/>

username said hello to you three <br/>

username said hello to you four <br/>

username said hello to you five <br/>

</div>

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153209
Share on other sites

Gotcha. I would still just treat it as a string and not use array functions. This code should do it. I separated

the variables to make it easier to read but you could chain everything as well.

	
var div = 'chat';
var html = 'username said hello five <br/>';
var original = document.getElementById(div).innerHTML;
var index = original.indexOf('<br>') + 4;
var partial = original.substr(index);
document.getElementById(div).innerHTML = partial + html;

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153243
Share on other sites

Okay its sorta improved but its not cutting the string properly.

 

Also the <br/> no longer seems to be in the divs so nothing is going to new lines aka its just a big block of text.

 

Edit:

Also it doesn't limit it to the number of lines that is put in the JS either

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153256
Share on other sites

Here's my example. Is it not giving the correct result? http://gonavygolf.com/test9.html

 

"Also it doesn't limit it to the number of lines that is put in the JS either"

 

Do you mean it should be dynamic? The text can grow or shrink and the js should accomodate accordingly?

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153265
Share on other sites

It only shows 1 line - so when a new line comes in from a user only the one line shows no idea where the rest went.

 

This is what i got:

 

 

var data = eval('(' + this.responseText + ')');
                        
// start next timer
up.start(data.lastupdate);
                        
// fill chat
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;

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153267
Share on other sites

Okay here is the connection script:

 

function ajaxPost(url, postData, callback) {
    var req;
     try {
        req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (e) {
        // browser does not have ajax support
    }
    req.onreadystatechange = typeof callback == 'function' ? callback : function() {
        if (req.readyState == 4 && req.status == 200) {
            if(typeof callback == 'string') callback = document.getElementById(callback);
            if(callback) callback.innerHTML = req.responseText;
        }
    };
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(postData);

    return false;
}

 

And the script you have already seen:

 

    function Chat(div, url) {
        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
                            

                                var original = document.getElementById(up.div).innerHTML;
                                var index = original.indexOf('<br>') + 4;
                                var partial = original.substr(index);
                                alert(partial);
                                document.getElementById(up.div).innerHTML = partial + data.html;

                            //document.getElementById(up.div).innerHTML += data.html;
                            //alert(document.getElementById(up.div).innerHTML);
                        //alert(document.getElementById(div).innerHTML);
                        }
                        
                        // connection error
                        else {
                            // try again in .8 of a seconds
                            setTimeout(function(){
                                up.start(lastupdate);
                            }, 800);
                        }
                    }
                };
            }(this));
        };
    }
    
    var chat = new Chat('chat', 'backend.php');

 

 

Backend.php:

 

function getNewMessagesSince($timestamp) {
//get newest line (all of this stuff works so just removed it to keep it simple

        return array(
            'lastupdate' =>  time(),
            'html' => $row['Message'].' <br/>'
        );
}

while(! ($row = getNewMessagesSince($_POST['lastupdate']))) {
    //every half a second
    usleep(50000);
}

Link to comment
https://forums.phpfreaks.com/topic/223042-split-strings/#findComment-1153553
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.