Autor Zpráva
Baterie
Profil
Ahoj všichni, potřebuju vaší pomoc. Mám js script, který vypisuje každe 2s všechny řádky logu. Potřebuju přidat funkci, která ukáže pouze posledních 20 řádků, protože log je velký. Poradite jak? V php bych si to udělal sám, ale nemám podporu php na webu, takže potřebuju vaší pomoc.

Javascript soubor jquery.logviewer.js:
 (function(jQuery){
    
    var logViewer = function (options) {
        
        doLVHead = function(id) {
            jQuery.ajax({type: "HEAD",
                url: logViewer.options.logUrl,
                cache: false,
                complete: function(xhr, textStatus) {
                    if (textStatus == "success") {
                          var newLenght = parseInt(xhr.getResponseHeader("Content-Length")); 
                          checkLVLength(newLenght);
                    } 
                }
            });
        },
        
        checkLVLength = function (newLenght){
            if (logViewer.curLenght != newLenght) {
                if (logViewer.curLenght > newLenght) {
                    logViewer.curLenght = 0;
                    jQuery("#" + logViewer.options.targetObjectID).append('\nReseting ... \n');
                }
                var getBytes = logViewer.curLenght;
                var readBytes = parseInt(logViewer.options.readBytes);
                
                if (logViewer.curLenght == 0 && newLenght > readBytes) {
                    getBytes = newLenght - readBytes;
                } else if (logViewer.curLenght > 0) {
                    getBytes--;
                }
                 
                jQuery.ajax({type: "GET",
                    url: logViewer.options.logUrl,
                    cache: false,
                    success: function(data) {
                        data = logViewer.options.callback.call(this, data);
                        jQuery("#" + logViewer.options.targetObjectID).append(cleanLVtags(data));
                        var objDiv = document.getElementById(logViewer.options.targetObjectID);
                        objDiv.scrollTop = objDiv.scrollHeight;  
                    },
                    beforeSend: function(http){
                        http.setRequestHeader('Range', 'bytes='+getBytes+'-');
                    }
                });
            }
            logViewer.curLenght = newLenght; 
            setMyTimeOut();
        },

        setMyTimeOut = function(){
            if (logViewer.timeoutID > 0) {
                window.clearTimeout(logViewer.timeoutID);
            }
            logViewer.timeoutID = window.setTimeout(doLVHead, logViewer.options.refreshtimeout);
        },         
         
        cleanLVtags = function(html) {
            if (typeof(html) == 'string'){
                return html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
            } else {
                return html;
            }
        };
                    
        return { init: function(options) {
                if (!options ) var options = {};
                if (options.logUrl == undefined ) {
                    alert('Log URL missing');
                    return false;
                }
                if (options.refreshtimeout == undefined ) options.refreshtimeout = '2000';
                if (options.readBytes == undefined ) options.readBytes = 100;
                if (options.callback == undefined ) options.callback = function(data){ return data;};
                
                options.targetObjectID = jQuery(this).attr('id');
                
                logViewer.options = options;
                logViewer.curLenght = 0;
                logViewer.curLenght = 0;
                
                doLVHead();
            }
        };        
  }();
  jQuery.fn.extend({
    logViewer: logViewer.init
  });
})(jQuery)
 
soubor index.html:
 <html>
<head>
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE"/>
<meta http-equiv="EXPIRES" content="01 Jan 1970 00:00:00 GMT"/>
<meta http-equiv="PRAGMA" content="NO-CACHE"/>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery.logviewer.js"></script>
<script type="text/javascript">

jQuery(document).bind("ready", function() {
     jQuery('#logcontent').logViewer({logUrl: '../server.log'});
});
</script>
</head>
<body>
<textarea id="logcontent" WRAP="off" style="width: 98%; height: 98%;" autocomplete="off">

</textarea>
</body>
</html>

 
Gavier
Profil
Zkus použít pro čtení ze souboru něco jako:
        $.get('file.txt', function(data) {    
                var lines = data.split("\n");
                for(var i=lines.length-20;i<=lines.length;i++){
                $('#logcontent').append('lines[i] + '<br>');
                }
        });
$.get vyžaduje jquery
zdroj: http://stackoverflow.com/questions/4408707/jquery-read-text-file-from-file-system
Baterie
Profil
jo ale já se v js skoro vubec nevyznam, prosím nemůžete mi rovnou ukazat modifikaci mého kodu, kterou bych mohl hned použít? byl bych hrozně rád. já bych nad tím jinak laboroval hodiny.

Vaše odpověď

Mohlo by se hodit

Neumíte-li správně určit příčinu chyby, vkládejte odkazy na živé ukázky.
Užíváte-li nějakou cizí knihovnu, ukažte odpovídajícím, kde jste ji vzali.

Užitečné odkazy:

Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0